Skip to content

Commit

Permalink
Extend the log format to properly print error traces
Browse files Browse the repository at this point in the history
  • Loading branch information
Jalle19 committed Sep 26, 2024
1 parent 7684008 commit b266159
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
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
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 b266159

Please sign in to comment.