Skip to content
Snippets Groups Projects
Unverified Commit 607b3896 authored by Sigmund, Dominik's avatar Sigmund, Dominik
Browse files

Added Option to format the output

parent 239e1f94
No related branches found
No related tags found
1 merge request!1Update index.test.js, examples/all-options.js, examples/log-to-file.js,...
...@@ -29,10 +29,22 @@ e.g.: ...@@ -29,10 +29,22 @@ e.g.:
`log.info('this', ['array', 8], 4, {type:'test'})` `log.info('this', ['array', 8], 4, {type:'test'})`
will logged as will logged as (with default format)
__YYYY-MM-DDTHH:ii:ss SERVER INFO this ["array",8] 4 {"type": "test"} __YYYY-MM-DDTHH:ii:ss SERVER INFO this ["array",8] 4 {"type": "test"}
### Formatting
You can edit the output using the __format__-Option.
Possible Setting:
- __{timestamp}__: Display the timestamp in ISO format
- __{hostname}__: Display the hostname
- __{name}__: Display the Application name
- __{loglevel}__: Display the Loglevel
- __{message}__: Display the Message
### Options ### Options
```json ```json
...@@ -43,6 +55,7 @@ __YYYY-MM-DDTHH:ii:ss SERVER INFO this ["array",8] 4 {"type": "test"} ...@@ -43,6 +55,7 @@ __YYYY-MM-DDTHH:ii:ss SERVER INFO this ["array",8] 4 {"type": "test"}
"path": "Path Logfile located", "path": "Path Logfile located",
"loglevel": "Minimal LogLevel. Default: WARN", "loglevel": "Minimal LogLevel. Default: WARN",
"delimeter": "How to join multiple outputs. Default: (a single space)", "delimeter": "How to join multiple outputs. Default: (a single space)",
"format": "Format the output. Default: {timestamp}\\t{hostname}\\t{loglevel}\\t{message}",
"graylog": { "graylog": {
"active": "true or false", "active": "true or false",
"mode": "http or udp, defaults to udp", "mode": "http or udp, defaults to udp",
......
const Log = require('../index.js')
let log = new Log({
name: 'Sample-Application',
hostname: 'test-server',
loglevel: 'INFO'
})
log.info('This is an Information in default formatting')
log = new Log({
name: 'Sample-Application',
hostname: 'test-server',
loglevel: 'INFO',
format: '{message}'
})
console.log()
console.log('---- Message Only ----')
log.info('Only the message')
log = new Log({
name: 'Sample-Application',
hostname: 'test-server',
loglevel: 'INFO',
format: '{"timestamp":"{timestamp}", "level":"{loglevel}", "message":"{message}"}'
})
console.log()
console.log('---- As JSON ----')
log.info('JSON Output')
...@@ -13,6 +13,11 @@ function Log (options) { ...@@ -13,6 +13,11 @@ function Log (options) {
} else { } else {
this.loglevel = 2 this.loglevel = 2
} }
if (this.options.format) {
this.format = this.options.format
} else {
this.format = '{timestamp}\t{hostname}\t{name}\t{loglevel}\t{message}'
}
if (this.options.delimeter) { if (this.options.delimeter) {
this.delimeter = this.options.delimeter this.delimeter = this.options.delimeter
} else { } else {
...@@ -109,9 +114,13 @@ function Log (options) { ...@@ -109,9 +114,13 @@ function Log (options) {
} else { } else {
msgString = this.objectToString(message[0]) msgString = this.objectToString(message[0])
} }
let msg = this.format
msg = msg.replace('{timestamp}', this.getDate())
msg = msg.replace('{hostname}', this.hostname)
msg = msg.replace('{name}', this.name)
msg = msg.replace('{loglevel}', tag)
msg = msg.replace('{message}', msgString)
let msg = this.getDate() + '\t' + this.hostname + '\t' + this.name + '\t' + tag + '\t' + msgString
switch (tag) { switch (tag) {
case 'INFO': case 'INFO':
console.info(msg) console.info(msg)
......
...@@ -27,6 +27,7 @@ describe('@general/log', () => { ...@@ -27,6 +27,7 @@ describe('@general/log', () => {
expect(log.delimeter).toBe(' ') expect(log.delimeter).toBe(' ')
expect(log.name).toBe('@general/log') expect(log.name).toBe('@general/log')
expect(log.hostname).toBe(hostname) expect(log.hostname).toBe(hostname)
expect(log.format).toBe('{timestamp}\t{hostname}\t{name}\t{loglevel}\t{message}')
}) })
it('should use option hostname', () => { it('should use option hostname', () => {
let log = new LOG({ let log = new LOG({
...@@ -36,6 +37,7 @@ describe('@general/log', () => { ...@@ -36,6 +37,7 @@ describe('@general/log', () => {
expect(log.delimeter).toBe(' ') expect(log.delimeter).toBe(' ')
expect(log.name).toBe('@general/log') expect(log.name).toBe('@general/log')
expect(log.hostname).toBe('test') expect(log.hostname).toBe('test')
expect(log.format).toBe('{timestamp}\t{hostname}\t{name}\t{loglevel}\t{message}')
}) })
it('should use option name', () => { it('should use option name', () => {
let log = new LOG({ let log = new LOG({
...@@ -45,6 +47,7 @@ describe('@general/log', () => { ...@@ -45,6 +47,7 @@ describe('@general/log', () => {
expect(log.delimeter).toBe(' ') expect(log.delimeter).toBe(' ')
expect(log.name).toBe('test') expect(log.name).toBe('test')
expect(log.hostname).toBe(hostname) expect(log.hostname).toBe(hostname)
expect(log.format).toBe('{timestamp}\t{hostname}\t{name}\t{loglevel}\t{message}')
}) })
it('should use option delimeter', () => { it('should use option delimeter', () => {
let log = new LOG({ let log = new LOG({
...@@ -54,6 +57,7 @@ describe('@general/log', () => { ...@@ -54,6 +57,7 @@ describe('@general/log', () => {
expect(log.delimeter).toBe(';') expect(log.delimeter).toBe(';')
expect(log.name).toBe('@general/log') expect(log.name).toBe('@general/log')
expect(log.hostname).toBe(hostname) expect(log.hostname).toBe(hostname)
expect(log.format).toBe('{timestamp}\t{hostname}\t{name}\t{loglevel}\t{message}')
}) })
it('should use option loglevel', () => { it('should use option loglevel', () => {
let log = new LOG({ let log = new LOG({
...@@ -63,10 +67,19 @@ describe('@general/log', () => { ...@@ -63,10 +67,19 @@ describe('@general/log', () => {
expect(log.delimeter).toBe(' ') expect(log.delimeter).toBe(' ')
expect(log.name).toBe('@general/log') expect(log.name).toBe('@general/log')
expect(log.hostname).toBe(hostname) expect(log.hostname).toBe(hostname)
expect(log.format).toBe('{timestamp}\t{hostname}\t{name}\t{loglevel}\t{message}')
})
it('should use option format', () => {
let log = new LOG({
format: '{message}'
});
expect(log.loglevel).toBe(2)
expect(log.delimeter).toBe(' ')
expect(log.name).toBe('@general/log')
expect(log.hostname).toBe(hostname)
expect(log.format).toBe('{message}')
}) })
it.todo('should use option graylog') it.todo('should use option graylog')
it.todo('should use option file')
it.todo('should use option path')
}) })
describe('log by level', () => { describe('log by level', () => {
it('should log fatal only if level is appropriate', () => { it('should log fatal only if level is appropriate', () => {
...@@ -270,6 +283,62 @@ describe('@general/log', () => { ...@@ -270,6 +283,62 @@ describe('@general/log', () => {
expect(logged).toBe(expected) expect(logged).toBe(expected)
}) })
}) })
describe('log formatting', () => {
it('should log message only if set', () => {
let log = new LOG({
format: '{message}',
loglevel: 'DEBUG'
});
let logged = log.debug('test')
let expected = 'test'
expect(logged).toBe(expected)
})
it('should log hostname only if set', () => {
let log = new LOG({
format: '{hostname}',
loglevel: 'DEBUG'
});
let logged = log.debug('test')
let expected = hostname
expect(logged).toBe(expected)
})
it('should log name only if set', () => {
let log = new LOG({
format: '{name}',
loglevel: 'DEBUG'
});
let logged = log.debug('test')
let expected = '@general/log'
expect(logged).toBe(expected)
})
it('should log timestamp only if set', () => {
let log = new LOG({
format: '{timestamp}',
loglevel: 'DEBUG'
});
let logged = log.debug('test')
let expected = getDate()
expect(logged).toBe(expected)
})
it('should log loglevel only if set', () => {
let log = new LOG({
format: '{loglevel}',
loglevel: 'DEBUG'
});
let logged = log.debug('test')
let expected = 'DEBUG'
expect(logged).toBe(expected)
})
it('should log json like message if set', () => {
let log = new LOG({
format: '{"timestamp":"{timestamp}", "level":"{loglevel}", "message":"{message}"}',
loglevel: 'DEBUG'
});
let logged = log.debug('test')
let expected = '{"timestamp":"' + getDate() + '", "level":"DEBUG", "message":"test"}'
expect(logged).toBe(expected)
})
})
describe('log to file', () => { describe('log to file', () => {
it('should create the folder if not exists', () => { it('should create the folder if not exists', () => {
let logPath = os.tmpdir() + '/log-test' + Math.floor(Math.random() * 1000) let logPath = os.tmpdir() + '/log-test' + Math.floor(Math.random() * 1000)
...@@ -312,7 +381,6 @@ describe('@general/log', () => { ...@@ -312,7 +381,6 @@ describe('@general/log', () => {
it.todo('should log to graylog via udp') it.todo('should log to graylog via udp')
it.todo('should log to graylog via http') it.todo('should log to graylog via http')
}) })
// TODO: formatting
// TODO: log to loki // TODO: log to loki
// TODO: add mutation test // TODO: add mutation test
}) })
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment