Security News
UK Officials Consider Banning Ransomware Payments from Public Entities
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.
log-segment
Advanced tools
Javascript logger with partition
Why Another Log Module? Because not all logs are equal.
I'd like to use a pliable tool to quickly manage logs by two factors: level and segment.
$ npm i log-segment
log-segment
is single-ton, so just require and use everywhere.
const log = require('log-segment')
const sql = 'INSERT INTO table ...'
log.info('sql', 'executing query ...', log.value('sql', sql))
db.query(sql)
.then(() => {
log.success('sql', 'query done.', log.value('sql', sql))
})
.catch((err) => {
log.error('sql', 'query error', log.value('sql', sql))
})
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' } },
template: '{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'
}
}
})
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({
segments: {
http: {
color: 'yellow'
},
sql: {
color: 'white'
}
},
levels: {
trace: {
marker: '[TRACE]'
},
warning: {
marker: '[WARN]'
},
error: {
marker: '[ERR]'
}
}
})
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 '{marker} [{timestamp}] {message}'
.
You can also add {trace}
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.MAIL,
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.3.0
v. 1.2.0
v. 1.1.0
log.chrono('tag')
console.log('%c message', 'color: red');
The MIT License (MIT)
Copyright (c) 2017, 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 33 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
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.