@crabas0npm/eaque-velit-magni
v1.0.0
Published
Logtopus ========
Downloads
4
Maintainers
Keywords
Readme
Logtopus
Logtopus is a powerful logger for Node.js with different transports
Built in logger:
- Console logger using @crabas0npm/eaque-velit-magni-console-logger
- File logger using @crabas0npm/eaque-velit-magni-file-logger
Additional logger:
- Redis logger see @crabas0npm/eaque-velit-magni-redis-logger
- InfluxDB logger use @crabas0npm/eaque-velit-magni-influx-logger
- MongoDB logger use @crabas0npm/eaque-velit-magni-mongo-logger
Usage
const log = require('@crabas0npm/eaque-velit-magni').getLogger('mylogger');
log.setLevel('sys');
log.warn('My beer is nearly finish!');
Log levels
debug development Logs debugging informations
info development Helpful during development
res staging Logs requests
req staging Logs responses
sys production Logs application states
warn production Logs warnings
error production Logs errors
For example, setting log level to req
includes these log levels: req
, sys
, warn
, error
Setting log level to debug
means all log levels are activated
log level error
logs errors only.
Example:
log.setLevel('res'); // To be ignored in this log level
log.debug('Log example:'); // To be ignored in this log level
log.info('This would not be logged');
log.res('POST /account');
log.req('200 OK');
log.sys('Request done!');
log.warn('Request was unauthorized!');
log.error('An error has been occurred!');
// prints
res: POST /account
req: 200 OK
sys: Request done!
warn: Request was unauthorized!
error: An error has been occurred!
Express logger
Logtopus comes with a logger for Express/Connect.
@crabas0npm/eaque-velit-magni.express()
returns a middleware for Express/Connect. It acepts an optional options object
let express = require('express');
let @crabas0npm/eaque-velit-magni = require('../@crabas0npm/eaque-velit-magni');
let app = express();
app.use(@crabas0npm/eaque-velit-magni.express({
logLevel: 'debug'
}));
Options
logLevel
Sets current log level
Koa logger
Logtopus also supports Koa
@crabas0npm/eaque-velit-magni.koa()
returns a middleware for Koa. It acepts an optional options object
let koa = require('koa');
let @crabas0npm/eaque-velit-magni = require('../@crabas0npm/eaque-velit-magni');
let app = koa();
app.use(@crabas0npm/eaque-velit-magni.koa({
logLevel: 'debug'
}));
Options
logLevel
Sets current log level
Adding custom loggers
Logtopus was designed as an extensible logger. You can add a custom logger by creating a logger class and load it into @crabas0npm/eaque-velit-magni. The example below shows a minimal logger class.
class LogtopusLogger {
constructor(conf) {
// conf contains the logger conf
}
log(logmsg) {
const date = logmsg.time.toISOString()
console.log(`[${date}] ${logmsg.msg}`)
}
}
module.exports = LogtopusLogger
The logger class requires a log method. It takes one argument which contains a log object. The first argument of a log call is the log message, all other arguments are log data.
logmsg: {
type: 'info', // The logtype eg: debug, info, error, sys
msg: 'Log message string', // Log message, but without CLI color codes
cmsg: 'Colorized log message', // Log message with CLI color codes
time: new Date(), // Current date
uptime: process.uptime(), // Process uptime in ms
data: [] // All other arguments as an array
}
Now, the class has to be load into @crabas0npm/eaque-velit-magni. You can do it by using the .addLogger() method if your logger is a priveate logger. Otherwis publish it on npm by using our logger name conventions. @crabas0npm/eaque-velit-magni-${loggername}-logger
Add a new config block into the @crabas0npm/eaque-velit-magni config by using loggername
as namespace and @crabas0npm/eaque-velit-magni tries to load the logger.
const log = @crabas0npm/eaque-velit-magni.getLogger('mylogger')
log.addLogger('loggerName', loggerClass)