Skip to content

Commit

Permalink
feat(logging): add option to configure JSON logging (#5022)
Browse files Browse the repository at this point in the history
* fix: Use logger for logs using console logging

* feat: Add option to configure JSON logging

* fix: use generic logFormat instead of json specific

* fix: use logFormat for docker config

* fix: use logFormat to build winston formatters

Co-authored-by: Nicolas Giard <github@ngpixel.com>
  • Loading branch information
mskrip and NGPixel committed Feb 19, 2022
1 parent 69e9ccc commit 2815f38
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 9 deletions.
7 changes: 7 additions & 0 deletions config.sample.yml
Expand Up @@ -108,6 +108,13 @@ bindIP: 0.0.0.0

logLevel: info

# ---------------------------------------------------------------------
# Log Format
# ---------------------------------------------------------------------
# Output format for logging, possible values: default, json

logFormat: default

# ---------------------------------------------------------------------
# Offline Mode
# ---------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions dev/build/config.yml
Expand Up @@ -16,4 +16,5 @@ ssl:
domain: $(LETSENCRYPT_DOMAIN)
subscriberEmail: $(LETSENCRYPT_EMAIL)
logLevel: info
logFormat: $(LOG_FORMAT)
ha: $(HA_ACTIVE)
2 changes: 1 addition & 1 deletion server/core/kernel.js
Expand Up @@ -19,7 +19,7 @@ module.exports = {
} catch (err) {
WIKI.logger.error('Database Initialization Error: ' + err.message)
if (WIKI.IS_DEBUG) {
console.error(err)
WIKI.logger.error(err)
}
process.exit(1)
}
Expand Down
21 changes: 14 additions & 7 deletions server/core/logger.js
Expand Up @@ -6,14 +6,21 @@ const winston = require('winston')
module.exports = {
loggers: {},
init(uid) {
let logger = winston.createLogger({
const loggerFormats = [
winston.format.label({ label: uid }),
winston.format.timestamp()
]

if (WIKI.config.logFormat === 'json') {
loggerFormats.push(winston.format.json())
} else {
loggerFormats.push(winston.format.colorize())
loggerFormats.push(winston.format.printf(info => `${info.timestamp} [${info.label}] ${info.level}: ${info.message}`))
}

const logger = winston.createLogger({
level: WIKI.config.logLevel,
format: winston.format.combine(
winston.format.colorize(),
winston.format.label({ label: uid }),
winston.format.timestamp(),
winston.format.printf(info => `${info.timestamp} [${info.label}] ${info.level}: ${info.message}`)
)
format: winston.format.combine(...loggerFormats)
})

// Init Console (default)
Expand Down
2 changes: 1 addition & 1 deletion server/modules/storage/git/storage.js
Expand Up @@ -73,7 +73,7 @@ module.exports = {
mode: 0o600
})
} catch (err) {
console.error(err)
WIKI.logger.error(err)
throw err
}
}
Expand Down

0 comments on commit 2815f38

Please sign in to comment.