diff --git a/src/eachwatt.ts b/src/eachwatt.ts index 9755901..4872454 100644 --- a/src/eachwatt.ts +++ b/src/eachwatt.ts @@ -91,7 +91,7 @@ const mainPollerFunc = async (config: Config) => { publisherImpl.publishCharacteristicsSensorData(characteristicsSensorData), ]) } catch (e) { - logger.error((e as Error).message) + logger.error(e) } } } 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/iotawatt.ts b/src/sensor/iotawatt.ts index 3d011af..188b84c 100644 --- a/src/sensor/iotawatt.ts +++ b/src/sensor/iotawatt.ts @@ -126,7 +126,7 @@ export const getSensorData: PowerSensorPollFunction = async ( powerFactor: getSensorPowerFactorValue(sensor, configuration, status), } } catch (e) { - logger.error((e as Error).message) + logger.error(e) return emptySensorData(timestamp, circuit) } } @@ -152,7 +152,7 @@ export const getCharacteristicsSensorData: CharacteristicsSensorPollFunction = a frequency: query[0][1], } } catch (e) { - logger.error((e as Error).message) + logger.error(e) return emptyCharacteristicsSensorData(timestamp, characteristics) } } 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) } }