Skip to content

Commit

Permalink
Merge pull request #80 from Jalle19/error-traces
Browse files Browse the repository at this point in the history
Print stack trace when logging errors
  • Loading branch information
Jalle19 authored Sep 30, 2024
2 parents 4fdf49a + b266159 commit 7e7858d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/eachwatt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const mainPollerFunc = async (config: Config) => {
publisherImpl.publishCharacteristicsSensorData(characteristicsSensorData),
])
} catch (e) {
logger.error((e as Error).message)
logger.error(e)
}
}
}
Expand Down
15 changes: 13 additions & 2 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
4 changes: 2 additions & 2 deletions src/sensor/iotawatt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -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)
}
}
8 changes: 2 additions & 6 deletions src/sensor/shelly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -197,7 +193,7 @@ export const getCharacteristicsSensorData: CharacteristicsSensorPollFunction = a
frequency: frequency,
}
} catch (e) {
logError(url, e)
logger.error(e)
return emptyCharacteristicsSensorData(timestamp, characteristics)
}
}

0 comments on commit 7e7858d

Please sign in to comment.