diff --git a/src/logger.ts b/src/logger.ts index 4979004..d899753 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -8,8 +8,19 @@ export enum LogLevel { // Define log transports here, so we can change the log level later const transports = [new winston.transports.Console()] -const logFormat = winston.format.printf(({ level, message, label, timestamp }) => { - return `${timestamp} [${label}] ${level}: ${message}` +const logFormat = winston.format.printf(({ level, message, label, timestamp, stack }) => { + // Stack should be either an empty string (for non-errors) or + // the original value minus the first line (which is not part of the + // trace itself) + + if (stack !== undefined) { + const st = stack as string + stack = st.substring(st.indexOf('\n')) + } else { + stack = '' + } + + return `${timestamp} [${label}] ${level}: ${message} ${stack}` }) export const setLogLevel = (level: LogLevel) => { diff --git a/src/sensor/shelly.ts b/src/sensor/shelly.ts index 28f261c..be2172b 100644 --- a/src/sensor/shelly.ts +++ b/src/sensor/shelly.ts @@ -46,10 +46,6 @@ type Gen2EMGetStatusResult = { const logger = createLogger('sensor.shelly') -const logError = (url: string, err: unknown): void => { - logger.error(`${url}: ${(err as Error).message}`) -} - const getSensorDataUrl = (sensor: ShellySensor | ShellyCharacteristicsSensor): string => { const address = sensor.shelly.address const meter = sensor.shelly.meter @@ -152,7 +148,7 @@ export const getSensorData: PowerSensorPollFunction = async ( return await parseGen2EMResponse(timestamp, circuit, httpResponse) } } catch (e) { - logError(url, e) + logger.error(e) return emptySensorData(timestamp, circuit) } } @@ -197,7 +193,7 @@ export const getCharacteristicsSensorData: CharacteristicsSensorPollFunction = a frequency: frequency, } } catch (e) { - logError(url, e) + logger.error(e) return emptyCharacteristicsSensorData(timestamp, characteristics) } }