log-segment
Advanced tools
Comparing version 1.2.0 to 1.3.0
118
main.js
@@ -8,2 +8,4 @@ 'use strict' | ||
// ? Error.stackTraceLimit = Infinity; | ||
const mode = { | ||
@@ -45,2 +47,8 @@ CONSOLE: 0, | ||
let __segments = {'*': { color: 'white' }} | ||
let __template = { | ||
string: '{marker} [{timestamp}] {message}', | ||
timestamp: true, | ||
marker: true, | ||
trace: false | ||
} | ||
@@ -66,2 +74,3 @@ let __markers | ||
* @param {Object} params | ||
* @param {string} template default '{marker} [{timestamp}] {message}' | ||
*/ | ||
@@ -91,2 +100,6 @@ const __init = function (params) { | ||
if (params.format) { | ||
__setTemplate(params.format) | ||
} | ||
if (params.enabled) { | ||
@@ -114,3 +127,4 @@ if (params.enabled.segments === null) { | ||
segments: Object.assign({}, __segments), | ||
enabled: Object.assign({}, __enabled) | ||
enabled: Object.assign({}, __enabled), | ||
format: __template.string | ||
} | ||
@@ -160,2 +174,9 @@ } | ||
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 __setSegments = function (segments) { | ||
@@ -277,4 +298,5 @@ __segments = {} | ||
let _data = Array.prototype.slice.call(arguments) | ||
_data = _data.map((message) => { | ||
const _data = {} | ||
_data.message = Array.prototype.slice.call(arguments).map((message) => { | ||
// stringify an object | ||
@@ -292,8 +314,3 @@ if (typeof message === 'object') { | ||
// paint the message | ||
if (__segments[segment] && | ||
__segments[segment].color && | ||
chalk[__segments[segment].color]) { | ||
return chalk[__segments[segment].color](message) | ||
} | ||
return message | ||
return __color(segment, message) | ||
}) | ||
@@ -303,19 +320,58 @@ | ||
if (__levels[level].marker) { | ||
_data.unshift(__markers[level]) | ||
_data.marker = __markers[level] | ||
} | ||
return __output(segment, level, _data) | ||
return __output(segment, level, __color(segment, __format(_data))) | ||
} | ||
} | ||
const __color = function (segment, message) { | ||
if (__segments[segment] && | ||
__segments[segment].color && | ||
chalk[__segments[segment].color]) { | ||
return chalk[__segments[segment].color](message) | ||
} | ||
return message | ||
} | ||
/** | ||
* @todo custom format | ||
* @param {Object} data {message, marker} | ||
* @return string | ||
*/ | ||
const __output = function (segment, level, data) { | ||
const __format = function (data) { | ||
if (__template.timestamp) { | ||
data.timestamp = __timestamp() | ||
} | ||
if (__template.trace) { | ||
data.trace = __trace() | ||
} | ||
data.message = data.message.join(' ') | ||
return tools.string.template(__template.string, data, true) | ||
} | ||
/** | ||
* nb heavy operation | ||
*/ | ||
const __timestamp = function () { | ||
return new Date().toISOString() | ||
} | ||
/** | ||
* nb heavy operation | ||
* nb split last 4 entries | ||
*/ | ||
const __trace = function () { | ||
return '\n' + (new Error().stack.split('\n').splice(4).join('\n')) | ||
} | ||
/** | ||
* @param {string} message | ||
*/ | ||
const __output = function (segment, level, message) { | ||
if (__segments[segment] && __segments[segment].mode === mode.EMAIL) { | ||
return __outputEmail(__segments[segment].email, data) | ||
return __outputEmail(__segments[segment].email, message) | ||
} | ||
if (__levels[level] && __levels[level].mode === mode.EMAIL) { | ||
return __outputEmail(__levels[level].email, data) | ||
return __outputEmail(__levels[level].email, message) | ||
} | ||
@@ -325,14 +381,17 @@ | ||
if (__segments[segment] && __segments[segment].mode === mode.FILE) { | ||
return __outputFile(__segments[segment].file, data) | ||
return __outputFile(__segments[segment].file, message) | ||
} | ||
if (__levels[level].mode === mode.FILE) { | ||
return __outputFile(__levels[level].file, data) | ||
return __outputFile(__levels[level].file, message) | ||
} | ||
// console | ||
return __outputConsole(data) | ||
return __outputConsole(message) | ||
} | ||
const __outputConsole = function (data) { | ||
console.log.apply(console, data) | ||
/** | ||
* @param {string} message | ||
*/ | ||
const __outputConsole = function (message) { | ||
console.log(message) | ||
return true | ||
@@ -343,5 +402,5 @@ } | ||
* @param {string} file /path/to/file | ||
* @param {string[]} data | ||
* @param {string} message | ||
*/ | ||
const __outputFile = function (file, data) { | ||
const __outputFile = function (file, message) { | ||
// open stream, if not already opened | ||
@@ -370,3 +429,3 @@ if (!__files[file]) { | ||
*/ | ||
__outputFile(file, data) | ||
__outputFile(file, message) | ||
}) | ||
@@ -376,4 +435,3 @@ return true | ||
data.push('\n') | ||
__files[file].stream.write(data.join(' ')) | ||
__files[file].stream.write(message + '\n') | ||
return true | ||
@@ -383,7 +441,7 @@ } | ||
/** | ||
* @todo html | ||
* @todo html? | ||
* @param {*} email | ||
* @param {*} data | ||
* @param {string} message | ||
*/ | ||
const __outputEmail = function (email, data) { | ||
const __outputEmail = function (email, message) { | ||
if (!email._transporter) { | ||
@@ -394,3 +452,3 @@ email._transporter = nodemailer.createTransport(email.transporter) | ||
const _options = Object.assign(email.options) | ||
_options.text = data.join('\n') | ||
_options.text = message + '\n' | ||
@@ -400,3 +458,3 @@ email._transporter.sendMail(_options, (err, info) => { | ||
__outputConsole(['ERROR SENDING EMAIL', _options]) | ||
__outputConsole([data]) | ||
__outputConsole(message) | ||
} | ||
@@ -403,0 +461,0 @@ }) |
{ | ||
"name": "log-segment", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "logger with partition", | ||
"main": "main.js", | ||
"dependencies": { | ||
"a-toolbox": "^0.1.x", | ||
"a-toolbox": "^0.x", | ||
"chalk": "^1.x", | ||
"fs-extra": "^2.x", | ||
"fs-extra": "^3.x", | ||
"nodemailer": "^4.x" | ||
@@ -14,3 +14,3 @@ }, | ||
"pre-commit": "^x", | ||
"tap": "^x", | ||
"tap": "^10.x", | ||
"standard": "^x" | ||
@@ -17,0 +17,0 @@ }, |
@@ -15,3 +15,3 @@ # log-segment | ||
Why Another Log Module? Because not all logs are equal. | ||
I'd like to use a pliable tool that allow to easy and quickly manage logs by two factors: **level** and **segment**. | ||
I'd like to use a pliable tool to quickly manage logs by two factors: **level** and **segment**. | ||
@@ -35,6 +35,6 @@ ## Installing | ||
.then(() => { | ||
log.success('sql', 'query done.', log.value('sql', sql)) | ||
log.success('sql', 'query done.', log.value('sql', sql)) | ||
}) | ||
.catch((err) => { | ||
log.error('sql', 'query error', log.value('sql', sql)) | ||
log.error('sql', 'query error', log.value('sql', sql)) | ||
}) | ||
@@ -47,13 +47,13 @@ ```` | ||
require('express')().all('/*', (request, response) => { | ||
log.info('http', 'request', request.method, request.baseUrl) | ||
doSomething(request) | ||
.then((output) => { | ||
response.send(output) | ||
log.success('http', 'response on request', request.method, request.baseUrl) | ||
}) | ||
.catch((err) => { | ||
response.sendStatus(500) | ||
log.error('http', 'response on request', request.method, request.baseUrl, log.value('err', err)) | ||
}) | ||
log.info('http', 'request', request.method, request.baseUrl) | ||
doSomething(request) | ||
.then((output) => { | ||
response.send(output) | ||
log.success('http', 'response on request', request.method, request.baseUrl) | ||
}) | ||
.catch((err) => { | ||
response.sendStatus(500) | ||
log.error('http', 'response on request', request.method, request.baseUrl, log.value('err', err)) | ||
}) | ||
}) | ||
@@ -81,4 +81,6 @@ ```` | ||
segments: { '*': { color: 'white' } }, | ||
enabled: { segments: '*', levels: '*' } } | ||
```` | ||
template: '{marker} [{timestamp}] {message}', | ||
enabled: { segments: '*', levels: '*' } | ||
} | ||
```` | ||
@@ -154,3 +156,2 @@ ##### Custom segments | ||
const sql = 'INSERT INTO table ...' | ||
@@ -177,2 +178,13 @@ | ||
#### Custom format | ||
Default format is ``'{marker} [{timestamp}] {message}'``. | ||
You can also add ``{trace}`` | ||
````js | ||
log.set({ | ||
format: '{marker} [{timestamp}] {message} {trace}' | ||
}) | ||
```` | ||
### Use Cases | ||
@@ -270,2 +282,7 @@ | ||
v. 1.3.0 | ||
- Add customizable output format | ||
- Add message info: trace, timestamp | ||
v. 1.2.0 | ||
@@ -284,9 +301,9 @@ | ||
- add message info: trace, timestamp, chrono | ||
- custom format in message | ||
- custom format for each level (ex. add trace only in panic) | ||
- add chrono > ``log.chrono('tag')`` | ||
- browser support (mode: console - no colors, file - fs.html5) | ||
- custom format in log.value | ||
- custom mode: stream, (sms, telegram and whatever) | ||
- multiple mode for each setting (example: on panic send email, log to file, send sms, call mom) | ||
- multiple mode for each setting (example: on panic send email + log to file + send sms + call mom) | ||
- customizable action (example: on error run function) | ||
- browser support (browserify?) | ||
- (evaluate) support workers (as transport) | ||
@@ -293,0 +310,0 @@ - [pino](https://github.com/pinojs/pino) |
22507
519
335
+ Addedfs-extra@3.0.1(transitive)
+ Addedjsonfile@3.0.1(transitive)
+ Addeduniversalify@0.1.2(transitive)
- Removedfs-extra@2.1.2(transitive)
- Removedjsonfile@2.4.0(transitive)
Updateda-toolbox@^0.x
Updatedfs-extra@^3.x