log-segment
Advanced tools
Comparing version 1.6.1 to 1.7.0
83
main.js
@@ -47,6 +47,11 @@ 'use strict' | ||
let __template = { | ||
string: '{marker} [{timestamp}] {message}', | ||
timestamp: true, | ||
marker: true, | ||
trace: false | ||
default: { | ||
string: '{segment} | {marker} [{timestamp}] {message}', | ||
segment: true, | ||
timestamp: true, | ||
marker: true, | ||
trace: false | ||
}, | ||
segment: {}, | ||
level: {} | ||
} | ||
@@ -100,3 +105,3 @@ | ||
if (params.format) { | ||
__setTemplate(params.format) | ||
__setTemplate('default', null, params.format) | ||
} | ||
@@ -144,6 +149,6 @@ | ||
return { | ||
levels: Object.assign({}, __levels), | ||
segments: Object.assign({}, __segments), | ||
enabled: Object.assign({}, __enabled), | ||
format: __template.string | ||
levels: tools.object.clone(__levels), | ||
segments: tools.object.clone(__segments), | ||
enabled: tools.object.clone(__enabled), | ||
format: __template.default.string | ||
} | ||
@@ -193,7 +198,16 @@ } | ||
const __setTemplate = function (template) { | ||
__template.string = template | ||
__template.marker = template.indexOf('{marker}') !== -1 | ||
__template.timestamp = template.indexOf('{timestamp}') !== -1 | ||
__template.trace = template.indexOf('{trace}') !== -1 | ||
const __setTemplate = function (part, name, template) { | ||
const _t = { | ||
string: template, | ||
segment: template.indexOf('{segment}') !== -1, | ||
marker: template.indexOf('{marker}') !== -1, | ||
timestamp: template.indexOf('{timestamp}') !== -1, | ||
trace: template.indexOf('{trace}') !== -1 | ||
} | ||
if (part === 'default') { | ||
__template.default = _t | ||
} else { | ||
__template[part][name] = _t | ||
} | ||
} | ||
@@ -213,2 +227,5 @@ | ||
__checkSetting(_segment, {segment: i}) | ||
if (_segment.format) { | ||
__setTemplate('segment', i, _segment.format) | ||
} | ||
__segments[i] = _segment | ||
@@ -246,2 +263,5 @@ } | ||
} | ||
if (_level.format) { | ||
__setTemplate('level', i, _level.format) | ||
} | ||
__levels[i] = _level | ||
@@ -320,4 +340,6 @@ } | ||
const _data = {} | ||
const _args = Array.prototype.slice.call(arguments) | ||
_data.message = Array.prototype.slice.call(arguments).map((message) => { | ||
_data.segment = _args.shift(_args) | ||
_data.message = _args.map((message) => { | ||
// stringify an object | ||
@@ -343,3 +365,3 @@ if (typeof message === 'object') { | ||
return __output(segment, level, __color(segment, __format(_data))) | ||
return __output(segment, level, __color(segment, __format(segment, level, _data))) | ||
} | ||
@@ -358,14 +380,21 @@ } | ||
/** | ||
* NB segment has priority | ||
* @param {string} segment segment name | ||
* @param {string} level level name | ||
* @param {Object} data {message, marker} | ||
* @return string | ||
*/ | ||
const __format = function (data) { | ||
if (__template.timestamp) { | ||
const __format = function (segment, level, data) { | ||
const _t = __template.segment[segment] || __template.level[level] || __template.default | ||
if (_t.timestamp) { | ||
data.timestamp = __timestamp() | ||
} | ||
if (__template.trace) { | ||
if (_t.trace) { | ||
data.trace = __trace() | ||
} | ||
if (_t.segment) { | ||
data.segment = segment | ||
} | ||
data.message = data.message.join(' ') | ||
return tools.string.template(__template.string, data, true) | ||
return tools.string.template(_t.string, data, true) | ||
} | ||
@@ -467,3 +496,3 @@ | ||
const _options = Object.assign(email.options) | ||
const _options = tools.object.clone(email.options) | ||
_options.text = message + '\n' | ||
@@ -572,3 +601,3 @@ | ||
const _options = Object.assign(setting.email.options) | ||
const _options = tools.object.clone(setting.email.options) | ||
_options.text = '*** log-segment.check settings: mode email ***' | ||
@@ -586,2 +615,6 @@ | ||
/** | ||
* @param {!string} tag | ||
* @param {?boolean} reset | ||
*/ | ||
const chrono = function (tag, reset) { | ||
@@ -599,3 +632,3 @@ if (!__chrono[tag] || reset) { | ||
get: () => { | ||
return Object.assign({}, __levels) | ||
return tools.object.clone(__levels) | ||
} | ||
@@ -605,3 +638,3 @@ }) | ||
get: () => { | ||
return Object.assign({}, __segments) | ||
return tools.object.clone(__segments) | ||
} | ||
@@ -611,3 +644,3 @@ }) | ||
get: () => { | ||
return Object.assign({}, __enabled) | ||
return tools.object.clone(__enabled) | ||
} | ||
@@ -614,0 +647,0 @@ }) |
{ | ||
"name": "log-segment", | ||
"version": "1.6.1", | ||
"version": "1.7.0", | ||
"description": "logger with partition", | ||
"main": "main.js", | ||
"dependencies": { | ||
"a-toolbox": "^0.x", | ||
"a-toolbox": "^1.x", | ||
"chalk": "^1.x", | ||
@@ -9,0 +9,0 @@ "fs-extra": "^3.x", |
@@ -12,4 +12,3 @@ # log-segment | ||
Not all logs are equal. | ||
I'd like to use a pliable tool to quickly manage logs by two factors: **level** and **segment**. | ||
Quickly manage logs by two **level** and **segment**. | ||
@@ -77,3 +76,3 @@ ## Install | ||
segments: { '*': { color: 'white' } }, | ||
format: '{marker} [{timestamp}] {message}', | ||
format: '{segment} | {marker} [{timestamp}] {message}', | ||
enabled: { segments: '*', levels: '*' } | ||
@@ -83,2 +82,4 @@ } | ||
![default](./doc/img/default.png "default") | ||
##### Custom segments | ||
@@ -100,3 +101,8 @@ | ||
file: '/tmp/myapp/sql.log' | ||
} | ||
}, | ||
sys: { | ||
mode: log.mode.FILE, | ||
file: '/tmp/myapp/sys.log', | ||
format: '{timestamp} {message}' | ||
}, | ||
} | ||
@@ -125,3 +131,3 @@ }) | ||
![custom segments](./doc/img/custom-segments.jpg "custom segments") | ||
![custom segments](./doc/img/custom-segments.png "custom segments") | ||
@@ -134,10 +140,2 @@ #### Custom levels | ||
log.set({ | ||
segments: { | ||
http: { | ||
color: 'yellow' | ||
}, | ||
sql: { | ||
color: 'white' | ||
} | ||
}, | ||
levels: { | ||
@@ -151,3 +149,6 @@ trace: { | ||
error: { | ||
marker: '[ERR]' | ||
marker: '[ERR]', | ||
mode: log.mode.FILE, | ||
file: '/tmp/myapp/error.log', | ||
format: '{timestamp} {message}' | ||
} | ||
@@ -176,8 +177,8 @@ } | ||
![custom levels](./doc/img/custom-levels.jpg "custom levels") | ||
![custom levels](./doc/img/custom-levels.png "custom levels") | ||
#### Custom format | ||
Default format is ``'{marker} [{timestamp}] {message}'``. | ||
You can also add ``{trace}`` | ||
Default format is ``'{segment} | {marker} [{timestamp}] {message}'``. | ||
You can optionally add ``{trace}`` | ||
@@ -265,3 +266,3 @@ ````js | ||
panic: { | ||
mode: log.mode.MAIL, | ||
mode: log.mode.EMAIL, | ||
email: { | ||
@@ -293,2 +294,7 @@ transporter: { | ||
v. 1.7.0 | ||
- Add custom format to ``level``, optionally add ``{segment}`` | ||
- Default format review as ``'{segment} | {marker} [{timestamp}] {message}'`` | ||
v. 1.6.0 | ||
@@ -300,11 +306,11 @@ | ||
- Add customizable output format | ||
- Add message info: trace, timestamp | ||
- Add customizable output default format | ||
- Add message info ``trace``, ``timestamp`` | ||
v. 1.2.0 | ||
- Add .check(): check settings for | ||
- Add ``.check()`` to check settings for | ||
- console > color | ||
- files > write permission | ||
- emails > send settings | ||
- emails > sending settings | ||
@@ -317,3 +323,2 @@ v. 1.1.0 | ||
- [ ] custom format for each level (ex. add trace only in panic) | ||
- [ ] browser support, only console mode (!colors) ``console.log('%c message', 'color: red');`` | ||
@@ -324,8 +329,8 @@ - [ ] custom mode: stream, (sms, telegram and whatever) | ||
- [ ] (evaluate) support workers (as transport) | ||
- [bunyan](https://github.com/trentm/node-bunyan) | ||
- [pino](https://github.com/pinojs/pino) | ||
- [npmlog](https://github.com/npm/npmlog) | ||
- [winston](https://github.com/winstonjs/winston) | ||
- [debug](https://github.com/visionmedia/debug) | ||
- [log](https://github.com/tj/log.js) | ||
- [debug](https://github.com/visionmedia/debug) | ||
- [bunyan](https://github.com/trentm/node-bunyan) | ||
- [npmlog](https://github.com/npm/npmlog) | ||
- others? | ||
@@ -339,3 +344,3 @@ | ||
Copyright (c) 2017, [braces lab](https://braceslab.com) | ||
Copyright (c) 2017-2018, [braces lab](https://braceslab.com) | ||
@@ -342,0 +347,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy |
24674
577
350
+ Addeda-toolbox@1.7.3(transitive)
+ Addedhash.js@1.1.7(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedminimalistic-assert@1.0.1(transitive)
- Removeda-toolbox@0.1.15(transitive)
Updateda-toolbox@^1.x