Skip to content

Commit dcaab80

Browse files
hardillbSteve-Mcl
andauthored
feat: Add JSON logging format (#647)
Co-authored-by: Stephen McLaughlin <44235289+Steve-Mcl@users.noreply.github.com>
1 parent 7a12355 commit dcaab80

4 files changed

Lines changed: 27 additions & 8 deletions

File tree

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ Extra options | Description
113113
`interval` | How often, in seconds, the agent checks in with the platform. Default: 60s
114114
`intervalJitter`| How much, in seconds, to vary the heartbeat +/- `intervalJitter`. Default: 10s
115115
`moduleCache` | If the device can not access npmjs.org then use the node modules cache in `module_cache` directory. Default `false`
116+
`verbose` | Enables verbose logging
117+
`jsonLogging` | Enabled JSON format logs
116118

117119
#### NodeJS options
118120

@@ -235,6 +237,8 @@ Extra options | Description
235237
----------------|---------------
236238
`interval` | How often, in seconds, the agent checks in with the platform. Default: 60s
237239
`intervalJitter`| How much, in seconds, to vary the heartbeat +/- `intervalJitter`. Default: 10s
240+
`verbose` | Enables verbose logging
241+
`jsonLogging` | Enabled JSON format logs
238242

239243
## Running
240244

@@ -278,9 +282,10 @@ Setup command
278282
279283
Global Options
280284
281-
-h, --help print out helpful usage information
282-
--version print out version information
283-
-v, --verbose turn on debugging output
285+
-h, --help print out helpful usage information
286+
--version print out version information
287+
-v, --verbose turn on debugging output
288+
-j, --json-logging print logs in JSON format
284289
```
285290

286291
## Running with no access to npmjs.org

lib/cli/args.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,5 +144,12 @@ module.exports = [
144144
multiple: true,
145145
typeLabel: '{underline string}',
146146
group: 'main'
147+
},
148+
{
149+
name: 'json-logging',
150+
description: 'output logs in JSON format',
151+
type: Boolean,
152+
alias: 'j',
153+
group: 'global'
147154
}
148155
]

lib/logging/log.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ const { hasProperty } = require('../utils')
44
let buffer
55
let mqtt
66
let verbose = false
7+
let jsonLog = false
78

89
function log (msg, level) {
910
const date = new Date()
10-
console.log(`[AGENT] ${date.toLocaleDateString()} ${date.toLocaleTimeString()} [${level || 'info'}] ${msg}`) // eslint-disable-line no-console
1111
const jsMsg = { level, msg }
12+
if (jsonLog) {
13+
console.log(JSON.stringify({ ...jsMsg, ts: Date.now() })) // eslint-disable-line no-console
14+
} else {
15+
console.log(`[AGENT] ${date.toLocaleDateString()} ${date.toLocaleTimeString()} [${level || 'info'}] ${msg}`) // eslint-disable-line no-console
16+
}
1217
if (buffer) {
1318
buffer.add(jsMsg)
1419
}
@@ -33,7 +38,11 @@ function NRlog (msg) {
3338
if (typeof jsMsg.msg !== 'string') {
3439
jsMsg.msg = JSON.stringify(jsMsg.msg)
3540
}
36-
console.log(`[NR] ${date.toLocaleDateString()} ${date.toLocaleTimeString()} [${jsMsg.level || 'info'}] ${jsMsg.msg}`) // eslint-disable-line no-console
41+
if (jsonLog) {
42+
console.log(JSON.stringify({ ...jsMsg, ts: Date.now() })) // eslint-disable-line no-console
43+
} else {
44+
console.log(`[NR] ${date.toLocaleDateString()} ${date.toLocaleTimeString()} [${jsMsg.level || 'info'}] ${jsMsg.msg}`) // eslint-disable-line no-console
45+
}
3746
if (buffer) {
3847
buffer.add(jsMsg)
3948
}
@@ -55,6 +64,7 @@ module.exports = {
5564
initLogger: configuration => {
5665
verbose = configuration.verbose
5766
buffer = new LogBuffer(configuration.bufferSize || 1000)
67+
jsonLog = configuration.jsonLogging || false
5868
},
5969
info: msg => log(msg, 'info'),
6070
warn: msg => log(msg, 'warn'),

lib/logging/logBuffer.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ class LogBuffer {
1919
this.lastLogTimestampCount = 0
2020
}
2121
logEntry.ts = logEntry.ts + ('' + this.lastLogTimestampCount).padStart(4, '0')
22-
if (logEntry.level === 'system') {
23-
console.log(logEntry.msg) // eslint-disable-line no-console
24-
}
2522
this.buffer[this.head++] = logEntry
2623
if (this.head === this.size) {
2724
this.head = 0

0 commit comments

Comments
 (0)