Skip to content

Commit f9aa001

Browse files
authored
Merge pull request #1055 from SISheogorath/upgrade/winston
Upgrade winston / refactor logging
2 parents fc49326 + c358477 commit f9aa001

File tree

7 files changed

+25
-17
lines changed

7 files changed

+25
-17
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ There are some config settings you need to change in the files below.
179179
| `CMD_HOST` | `localhost` | host to listen on |
180180
| `CMD_PORT` | `80` | web app port |
181181
| `CMD_PATH` | `/var/run/codimd.sock` | path to UNIX domain socket to listen on (if specified, `CMD_HOST` and `CMD_PORT` are ignored) |
182+
| `CMD_LOGLEVEL` | `info` | Defines what kind of logs are provided to stdout. |
182183
| `CMD_ALLOW_ORIGIN` | `localhost, codimd.org` | domain name whitelist (use comma to separate) |
183184
| `CMD_PROTOCOL_USESSL` | `true` or `false` | set to use SSL protocol for resources path (only applied when domain is set) |
184185
| `CMD_URL_ADDPORT` | `true` or `false` | set to add port on callback URL (ports `80` or `443` won't be applied) (only applied when domain is set) |
@@ -274,6 +275,7 @@ There are some config settings you need to change in the files below.
274275
| `host` | `localhost` | host to listen on |
275276
| `port` | `80` | web app port |
276277
| `path` | `/var/run/codimd.sock` | path to UNIX domain socket to listen on (if specified, `host` and `port` are ignored) |
278+
| `loglevel` | `info` | Defines what kind of logs are provided to stdout. |
277279
| `allowOrigin` | `['localhost']` | domain name whitelist |
278280
| `useSSL` | `true` or `false` | set to use SSL server (if `true`, will auto turn on `protocolUseSSL`) |
279281
| `hsts` | `{"enable": true, "maxAgeSeconds": 31536000, "includeSubdomains": true, "preload": true}` | [HSTS](https://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security) options to use with HTTPS (default is the example value, max age is a year) |

config.json.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
}
77
},
88
"development": {
9+
"loglevel": "debug",
910
"hsts": {
1011
"enable": false
1112
},
@@ -16,6 +17,7 @@
1617
},
1718
"production": {
1819
"domain": "localhost",
20+
"loglevel": "info"
1921
"hsts": {
2022
"enable": true,
2123
"maxAgeSeconds": "31536000",

lib/config/default.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module.exports = {
77
urlPath: '',
88
host: '0.0.0.0',
99
port: 3000,
10+
loglevel: 'info',
1011
urlAddPort: false,
1112
allowOrigin: ['localhost'],
1213
useSSL: false,

lib/config/environment.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module.exports = {
99
host: process.env.CMD_HOST,
1010
port: toIntegerConfig(process.env.CMD_PORT),
1111
path: process.env.CMD_PATH,
12+
loglevel: process.env.CMD_LOGLEVEL,
1213
urlAddPort: toBooleanConfig(process.env.CMD_URL_ADDPORT),
1314
useSSL: toBooleanConfig(process.env.CMD_USESSL),
1415
hsts: {

lib/config/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ merge(config, require('./hackmdEnvironment'))
4545
merge(config, require('./environment'))
4646
merge(config, require('./dockerSecret'))
4747

48+
if (['debug', 'verbose', 'info', 'warn', 'error'].includes(config.loglevel)) {
49+
logger.level = config.loglevel
50+
} else {
51+
logger.error('Selected loglevel %s doesn\'t exist, using default level \'debug\'. Available options: debug, verbose, info, warn, error', config.loglevel)
52+
}
53+
4854
// load LDAP CA
4955
if (config.ldap.tlsca) {
5056
let ca = config.ldap.tlsca.split(',')

lib/logger.js

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
'use strict'
2-
const winston = require('winston')
2+
const {createLogger, format, transports} = require('winston')
33

4-
class Logger extends winston.Logger {
5-
// Implement stream.writable.write interface
6-
write (chunk) {
7-
this.info(chunk)
8-
}
9-
}
10-
11-
module.exports = new Logger({
4+
module.exports = createLogger({
5+
level: 'debug',
6+
format: format.combine(
7+
format.uncolorize(),
8+
format.timestamp(),
9+
format.align(),
10+
format.splat(),
11+
format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)
12+
),
1213
transports: [
13-
new winston.transports.Console({
14-
level: 'debug',
15-
handleExceptions: true,
16-
json: false,
17-
colorize: false,
18-
timestamp: true
14+
new transports.Console({
15+
handleExceptions: true
1916
})
2017
],
21-
emitErrs: true,
2218
exitOnError: false
2319
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
"velocity-animate": "^1.4.0",
130130
"visibilityjs": "^1.2.4",
131131
"viz.js": "^1.7.0",
132-
"winston": "^2.3.0",
132+
"winston": "^3.1.0",
133133
"ws": "^6.0.0",
134134
"xss": "^1.0.3"
135135
},

0 commit comments

Comments
 (0)