log-segment
Advanced tools
Comparing version 1.0.8 to 1.0.9
120
main.js
@@ -5,7 +5,16 @@ 'use strict' | ||
const chalk = require('chalk') | ||
const fs = require('fs') | ||
// @todo transport for each level | ||
// console, file, stream, email (telegram, sms ...) | ||
// @todo format | ||
// @todo custom format messages | ||
const mode = { | ||
CONSOLE: 0, | ||
FILE: 1 | ||
// STREAM | ||
// OTHERS (telegram, sms ...) | ||
} | ||
/** | ||
@@ -15,2 +24,3 @@ * @todo params.format | ||
const Log = function (params) { | ||
// default console | ||
let __levels = { | ||
@@ -43,3 +53,3 @@ '*': { | ||
let __markers = {} | ||
let __markers | ||
@@ -51,7 +61,13 @@ const __enabled = { | ||
const __init = function (params) { | ||
/** | ||
* files stream.Writable | ||
*/ | ||
const __files = {} | ||
function __init (params) { | ||
__markers = {} | ||
__setLevels(__levels) | ||
} | ||
const set = function (params) { | ||
function set (params) { | ||
if (!params) { | ||
@@ -88,3 +104,3 @@ return | ||
*/ | ||
const add = function (params) { | ||
function add (params) { | ||
if (!params) { | ||
@@ -102,3 +118,3 @@ return | ||
const value = function (label, value) { | ||
function value (label, value) { | ||
return function () { | ||
@@ -117,3 +133,3 @@ if (typeof value === 'object') { | ||
const __setSegments = function (segments) { | ||
function __setSegments (segments) { | ||
__segments = {} | ||
@@ -123,3 +139,3 @@ __addSegments(segments) | ||
const __addSegments = function (segments) { | ||
function __addSegments (segments) { | ||
for (const i in segments) { | ||
@@ -137,3 +153,3 @@ if (__segments[i]) { | ||
const __setLevels = function (levels) { | ||
function __setLevels (levels) { | ||
// remove current levels | ||
@@ -149,3 +165,3 @@ for (const i in __levels) { | ||
const __addLevels = function (levels) { | ||
function __addLevels (levels) { | ||
for (const i in levels) { | ||
@@ -174,3 +190,7 @@ let _level = levels[i] | ||
const __print = function (level) { | ||
/** | ||
* | ||
* @param {string} level level name | ||
*/ | ||
function __print (level) { | ||
return function (segment) { | ||
@@ -181,4 +201,4 @@ if (!__check(segment, level)) { | ||
let _args = Array.prototype.slice.call(arguments) | ||
_args = _args.map((message) => { | ||
let _data = Array.prototype.slice.call(arguments) | ||
_data = _data.map((message) => { | ||
// stringify an object | ||
@@ -189,3 +209,3 @@ if (typeof message === 'object') { | ||
} catch (e) { | ||
message = 'INVALID-JSON' | ||
message = '[INVALID-JSON]' | ||
} | ||
@@ -207,14 +227,58 @@ } else if (typeof message === 'function') { | ||
if (__levels[level].marker) { | ||
_args.unshift(__markers[level]) | ||
_data.unshift(__markers[level]) | ||
} | ||
// @todo transport for each level | ||
// console, file, stream, email (telegram, sms ...) | ||
// @todo format | ||
console.log.apply(console, _args) | ||
return true | ||
return __output(segment, level, _data) | ||
} | ||
} | ||
const __check = function (segment, level) { | ||
function __output (segment, level, data) { | ||
// @todo custom format | ||
if (__segments[segment] && __segments[segment].mode === mode.FILE) { | ||
return __outputFile(__segments[segment].file, data) | ||
} | ||
if (__levels[level].mode === mode.FILE) { | ||
return __outputFile(__segments[segment].file, data) | ||
} | ||
// mode console | ||
console.log.apply(console, data) | ||
return true | ||
} | ||
/** | ||
* @todo open file err | ||
* @param {string} file /path/to/file | ||
* @param {string[]} data | ||
*/ | ||
function __outputFile (file, data) { | ||
// open stream, if not already opened | ||
if (!__files[file]) { | ||
__files[file] = fs.createWriteStream(file, {flags: 'a', defaultEncoding: 'utf8'}) | ||
/* debug | ||
__files[file].on('finish', function () { | ||
console.log('file has been written') | ||
}) | ||
__files[file].on('open', function () { | ||
console.log('file has been open') | ||
}) | ||
*/ | ||
process.on('beforeExit', () => { | ||
if (__files[file]) { | ||
__files[file].end() | ||
} | ||
}) | ||
} | ||
data.push('\n') | ||
return __files[file].write(data.join(' ')) | ||
} | ||
/** | ||
* @param {string} segment | ||
* @param {string} level | ||
*/ | ||
function __check (segment, level) { | ||
return ((segment === '*') || | ||
@@ -230,9 +294,15 @@ __enabled.segments === '*' || | ||
Object.defineProperty(Log.prototype, 'levels', { | ||
get: function () { return Object.assign({}, __levels) } | ||
get: () => { | ||
return Object.assign({}, __levels) | ||
} | ||
}) | ||
Object.defineProperty(Log.prototype, 'segments', { | ||
get: function () { return Object.assign({}, __segments) } | ||
get: () => { | ||
return Object.assign({}, __segments) | ||
} | ||
}) | ||
Object.defineProperty(Log.prototype, 'enabled', { | ||
get: function () { return Object.assign({}, __enabled) } | ||
get: () => { | ||
return Object.assign({}, __enabled) | ||
} | ||
}) | ||
@@ -242,2 +312,4 @@ Log.prototype.set = set | ||
Log.prototype.value = Log.prototype.val = Log.prototype.v = value | ||
Log.prototype.mode = mode | ||
} | ||
@@ -244,0 +316,0 @@ |
{ | ||
"name": "log-segment", | ||
"version": "1.0.8", | ||
"version": "1.0.9", | ||
"description": "logger with partition", | ||
@@ -5,0 +5,0 @@ "main": "main.js", |
@@ -32,2 +32,3 @@ # log-segment | ||
log.error('*', 'error message') | ||
log.panic('*', 'panic message') | ||
```` | ||
@@ -88,3 +89,3 @@ | ||
- custom transport by segment and level: console, file, stream, email (sms, telegram and whatever) | ||
- browser support (browserify) | ||
- browser support (browserify?) | ||
- (evaluate) support workers (as transport) | ||
@@ -91,0 +92,0 @@ - [pino](https://github.com/pinojs/pino) |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
10554
266
122
1