Security News
CISA Brings KEV Data to GitHub
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.
log-segment
Advanced tools
javascript logger with partition
Quickly manage logs by two level and segment.
$ npm i log-segment --save
log-segment
is singleton, so just require and use everywhere.
const log = require('log-segment')
const sql = 'INSERT INTO users (username, name) VALUES ($1, $2)'
log.info('sql', 'create user', log.value('sql', sql), log.chrono('insert-user'))
db.query(sql)
.then(() => {
log.success('sql', 'user created.', log.value('sql', sql), log.chrono('insert-user'))
})
.catch((err) => {
log.error('sql', 'user not created', log.value('sql', sql), log.value('error', err), log.chrono('insert-user'))
})
const log = require('log-segment')
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))
})
})
Default settings provide any levels and segments enabled to console output.
Default levels are: info, success, warning, error, panic.
There is no hierarchy by levels.
{
levels: {
'*': { color: 'white' },
info: { color: 'blue', marker: 'ℹ️' },
success: { color: 'green', marker: '✔' },
warning: { color: 'yellow', marker: '❗️️' },
error: { color: 'red', marker: '✗️' },
panic: { color: 'magenta', marker: '😱' }
},
segments: { '*': { color: 'white' } },
format: '{segment} | {marker} [{timestamp}] {message}',
enabled: { segments: '*', levels: '*' }
}
const log = require('log-segment')
log.set({
segments: {
http: {
color: 'cyan'
},
html: {
color: 'magenta'
},
sql: {
mode: log.mode.FILE,
file: '/tmp/myapp/sql.log'
},
sys: {
mode: log.mode.FILE,
file: '/tmp/myapp/sys.log',
format: '{timestamp} {message}'
},
}
})
const sql = 'INSERT INTO table ...'
log.info('sql', 'executing query ...', log.value('sql', sql))
log.success('sql', 'query done.', log.value('sql', sql))
const request = {
method: 'GET',
baseUrl: '/it/4x/api.html#req'
}
const err = new Error('file not found')
log.info('http', 'request', request.method, request.baseUrl)
log.error('http', 'response on request', request.method, request.baseUrl, log.value('err', err))
let username = null
log.warning('html', 'rendering missing value', log.value('username', username))
const log = require('log-segment')
log.set({
levels: {
trace: {
marker: '[TRACE]'
},
warning: {
marker: '[WARN]'
},
error: {
marker: '[ERR]',
mode: log.mode.FILE,
file: '/tmp/myapp/error.log',
format: '{timestamp} {message}'
}
}
})
const sql = 'INSERT INTO table ...'
log.trace('sql', 'executing query ...', log.value('sql', sql))
log.trace('sql', 'query done.', log.value('sql', sql))
const request = {
method: 'GET',
baseUrl: '/it/4x/api.html#req'
}
const err = new Error('file not found')
log.trace('http', 'request', request.method, request.baseUrl)
log.error('http', 'response on request', request.method, request.baseUrl, log.value('err', err))
let username = null
log.warning('html', 'rendering missing value', log.value('username', username))
Default format is '{segment} | {marker} [{timestamp}] {message}'
.
You can optionally add {trace}
to format and it will implicitly add trace information
log.set({
format: '{marker} [{timestamp}] {message} {trace}'
})
log.set({
segments: {
http: { color: 'cyan' },
network: { color: 'blue' },
db: { color: 'yellow' },
sql: { }
}
})
Development
Just enable everything on console
log.set({ enabled: { segments: '*', levels: '*' } })
Debug
Enable only segments to focus on, at any levels, to find that bug
log.set({ enabled: { segments: ['sql', 'network'] } })
Beta
Enable all segments, disable info and success level
log.set({ disabled: { levels: ['info', 'success'] } })
Production
Different behaviour for each level:
log.set({
enabled: {
segments: ['sql'],
levels: ['info', 'warning', 'error', 'panic']
},
segments: {
sql: {
mode: log.mode.FILE,
file: '/var/log/myapp/sql'
}
},
levels: {
info: {
marker: '[i]',
mode: log.mode.FILE,
file: '/var/log/myapp/info'
},
warning: {
marker: '[warn]',
mode: log.mode.FILE,
file: '/var/log/myapp/warn'
},
error: {
marker: '[err]',
mode: log.mode.FILE,
file: '/var/log/myapp/error'
},
panic: {
mode: log.mode.EMAIL,
email: {
transporter: {
service: 'gmail',
auth: {
user: '***@gmail.com',
pass: '***'
}
}
}
options: {
from: '"log-segment" <log-segment@test.test>',
to: 'sys-admin@gmail.com',
subject: 'myapp PANIC'
}
}
}
}
})
See documentation for further informations.
v. 1.7.0
level
, optionally add {segment}
'{segment} | {marker} [{timestamp}] {message}'
v. 1.6.0
log.chrono('tag')
v. 1.3.0
trace
, timestamp
v. 1.2.0
.check()
to check settings for
v. 1.1.0
console.log('%c message', 'color: red');
The MIT License (MIT)
Copyright (c) 2017-2018, braces lab
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
logger with partition
The npm package log-segment receives a total of 25 weekly downloads. As such, log-segment popularity was classified as not popular.
We found that log-segment demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.
Security News
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.