Comparing version 0.1.0 to 1.0.2
147
metalog.js
@@ -48,5 +48,53 @@ 'use strict'; | ||
// Logger wrapper to bind it to certain application | ||
class ApplicationLogger { | ||
// logger <Logger> | ||
// application <string> name | ||
constructor(logger, application = 'default') { | ||
this.logger = logger; | ||
this.application = application; | ||
} | ||
system(message) { | ||
this.logger.write('system', message, this.application); | ||
} | ||
fatal(message) { | ||
const msg = Logger.normalizeStack(message); | ||
this.logger.write('fatal', msg, this.application); | ||
} | ||
error(message) { | ||
const msg = Logger.normalizeStack(message); | ||
this.logger.write('error', msg, this.application); | ||
} | ||
warn(message) { | ||
this.logger.write('warn', message, this.application); | ||
} | ||
info(message) { | ||
this.logger.write('info', message, this.application); | ||
} | ||
debug(message) { | ||
const msg = Logger.normalizeStack(message); | ||
this.logger.write('debug', msg, this.application); | ||
} | ||
access(message) { | ||
this.logger.write('access', message, this.application); | ||
} | ||
slow(message) { | ||
this.logger.write('slow', message, this.application); | ||
} | ||
} | ||
// Logger constructor | ||
// path <string> log directory | ||
// nodeId <string> | ||
// node <string> nodeId | ||
// writeInterval <number> flush log to disk interval | ||
@@ -56,15 +104,10 @@ // writeBuffer <number> buffer size 64kb | ||
// toFile <string[]> write log types to file | ||
// stdout <string[]> stdout log types | ||
function Logger({ | ||
path, | ||
nodeId, | ||
writeInterval, | ||
writeBuffer, | ||
keepDays, | ||
toFile, | ||
stdout | ||
}) { | ||
// toStdout <string[]> write log types to stdout | ||
function Logger(options) { | ||
const { path, node } = options; | ||
const { writeInterval, writeBuffer, keepDays } = options; | ||
const { toFile, toStdout } = options; | ||
this.active = false; | ||
this.path = path; | ||
this.nodeId = nodeId; | ||
this.node = node; | ||
this.writeInterval = writeInterval || 3000; | ||
@@ -81,3 +124,3 @@ this.writeBuffer = writeBuffer || 64 * 1024; | ||
this.toFile = logTypes(toFile); | ||
this.stdout = logTypes(stdout); | ||
this.toStdout = logTypes(toStdout); | ||
this.open(); | ||
@@ -92,3 +135,3 @@ } | ||
const date = common.nowDate(); | ||
this.file = this.path + '/' + date + '-' + this.nodeId + '.log'; | ||
this.file = this.path + '/' + date + '-' + this.node + '.log'; | ||
const now = new Date(); | ||
@@ -132,4 +175,10 @@ const nextDate = new Date(); | ||
fs.stat(fileName, (err, stats) => { | ||
if (err || stats.size > 0) return; | ||
fs.unlink(this.file, () => {}); | ||
if (err) { | ||
console.log(err); | ||
return; | ||
} | ||
if (stats.size > 0) return; | ||
fs.unlink(this.file, (err) => { | ||
console.log(err); | ||
}); | ||
}); | ||
@@ -143,3 +192,6 @@ }); | ||
fs.readdir(this.path, (err, files) => { | ||
if (err) return; | ||
if (err) { | ||
console.log(err); | ||
return; | ||
} | ||
const now = new Date(); | ||
@@ -156,3 +208,5 @@ const date = new Date( | ||
if (fileAge > 1 && fileAge > this.keepDays - 1) { | ||
fs.unlink(this.path + '/' + fileName, common.emptiness); | ||
fs.unlink(this.path + '/' + fileName, (err) => { | ||
console.log(err); | ||
}); | ||
} | ||
@@ -169,51 +223,20 @@ } | ||
Logger.prototype.system = function(message) { | ||
this.write('system', message); | ||
}; | ||
Logger.prototype.fatal = function(message) { | ||
this.write('fatal', Logger.normalizeStack(message)); | ||
}; | ||
Logger.prototype.error = function(message) { | ||
this.write('error', Logger.normalizeStack(message)); | ||
}; | ||
Logger.prototype.warn = function(message) { | ||
this.write('warn', message); | ||
}; | ||
Logger.prototype.info = function(message) { | ||
this.write('info', message); | ||
}; | ||
Logger.prototype.debug = function(message) { | ||
this.write('debug', Logger.normalizeStack(message)); | ||
}; | ||
Logger.prototype.access = function(message) { | ||
this.write('access', message); | ||
}; | ||
Logger.prototype.slow = function(message) { | ||
this.write('slow', message); | ||
}; | ||
const pad = (s, len, char = ' ') => s + char.repeat(len - s.length); | ||
Logger.prototype.write = function(type, message) { | ||
const date = new Date().toISOString(); | ||
if (this.stdout[type]) { | ||
const line = ( | ||
textColor[type](date) + '\t' + | ||
typeColor[type](' ' + pad(type, 7)) + '\t' + | ||
textColor[type](message) | ||
); | ||
Logger.prototype.write = function(type, message, application = 'default') { | ||
const date = new Date(); | ||
if (this.toStdout[type]) { | ||
const normalColor = textColor[type]; | ||
const markColor = typeColor[type]; | ||
const time = normalColor(date.toTimeString().substring(0, 8)); | ||
const mark = markColor(' ' + pad(type, 7)); | ||
const msg = normalColor(`${this.node}/${application} ${message}`); | ||
const line = `${time} ${mark} ${msg}`; | ||
console.log(line); | ||
} | ||
if (this.toFile[type]) { | ||
const time = date.toISOString(); | ||
const multiline = (/[\n\r]/g).test(message); | ||
const line = multiline ? Logger.lineStack(message) : message; | ||
const data = date + '\t[' + type + ']\t' + line + '\n'; | ||
const data = `${time} [${type}] ${this.node}/${application} ${line}\n`; | ||
const buffer = Buffer.from(data); | ||
@@ -238,2 +261,6 @@ this.buffer.push(buffer); | ||
Logger.prototype.bind = function(application) { | ||
return new ApplicationLogger(this, application); | ||
}; | ||
module.exports = (args) => new Logger(args); |
{ | ||
"name": "metalog", | ||
"version": "0.1.0", | ||
"version": "1.0.2", | ||
"author": "Timur Shemsedinov <timur.shemsedinov@gmail.com>", | ||
@@ -34,3 +34,3 @@ "description": "Meta Logger for Metarhia", | ||
"devDependencies": { | ||
"eslint": "^5.10.0", | ||
"eslint": "^5.11.1", | ||
"metaschema": "0.0.29", | ||
@@ -37,0 +37,0 @@ "metatests": "^0.4.0" |
@@ -7,3 +7,4 @@ 'use strict'; | ||
path: './log', | ||
nodeId: 'S1N1', | ||
node: 'S1N1', | ||
application: 'app1', | ||
writeInterval: 3000, | ||
@@ -14,13 +15,19 @@ writeBuffer: 64 * 1024, | ||
logger.system('System test log message'); | ||
logger.fatal('Fatal test log message'); | ||
logger.error('Error test log message'); | ||
logger.warn('Warning test log message'); | ||
logger.info('Info test log message'); | ||
logger.debug('Debug test log message'); | ||
logger.access('Access test log message'); | ||
logger.slow('Slow test log message'); | ||
const appLogger = logger.bind('app'); | ||
const stack = new Error('Stack test log message').stack; | ||
logger.debug(stack); | ||
appLogger.debug(stack); | ||
appLogger.system('System test log message'); | ||
appLogger.fatal('Fatal test log message'); | ||
appLogger.error('Error test log message'); | ||
appLogger.warn('Warning test log message'); | ||
appLogger.info('Info test log message'); | ||
appLogger.debug('Debug test log message'); | ||
appLogger.access('Access test log message'); | ||
appLogger.slow('Slow test log message'); | ||
const defaultLogger = logger.bind(); | ||
defaultLogger.info('Write to default application logger'); | ||
logger.close(); | ||
@@ -8,8 +8,8 @@ 'use strict'; | ||
path: './log', | ||
nodeId: 'S1N1', | ||
node: 'S1N1', | ||
writeInterval: 3000, | ||
writeBuffer: 64 * 1024, | ||
keepDays: 5, | ||
stdout: [] | ||
}); | ||
toStdout: [] | ||
}).bind('app1'); | ||
@@ -19,10 +19,10 @@ const logger1 = createLogger(); | ||
metatests.test('logger.open', test => { | ||
logger1.on('open', () => { | ||
logger1.logger.on('open', () => { | ||
test.end(); | ||
}); | ||
logger1.on('error', err => { | ||
logger1.logger.on('error', err => { | ||
test.error(err); | ||
process.exit(1); | ||
}); | ||
logger1.open(); | ||
logger1.logger.open(); | ||
}); | ||
@@ -68,4 +68,4 @@ | ||
metatests.test('logger write more then 60Mb', test => { | ||
logger2.open(); | ||
logger2.stdout.INFO = false; | ||
logger2.logger.open(); | ||
logger2.logger.toStdout.INFO = false; | ||
const begin = process.hrtime(); | ||
@@ -75,9 +75,9 @@ for (let i = 0; i < 1000000; i++) { | ||
} | ||
logger2.close(); | ||
logger2.on('close', () => { | ||
logger2.logger.close(); | ||
logger2.logger.on('close', () => { | ||
const end = process.hrtime(begin); | ||
const time = end[0] * 1e9 + end[1]; | ||
logger2.open(); | ||
logger2.on('open', () => { | ||
logger2.stdout.INFO = true; | ||
logger2.logger.open(); | ||
logger2.logger.on('open', () => { | ||
logger2.logger.toStdout.INFO = true; | ||
logger2.info(time); | ||
@@ -92,6 +92,6 @@ test.end(); | ||
metatests.test('logger.close', test => { | ||
logger3.open(); | ||
logger3.logger.open(); | ||
logger3.info('Info log message'); | ||
logger3.close(); | ||
logger3.on('close', () => { | ||
logger3.logger.close(); | ||
logger3.logger.on('close', () => { | ||
test.end(); | ||
@@ -104,9 +104,9 @@ }); | ||
metatests.test('logger.close after close', test => { | ||
logger4.open(); | ||
logger4.logger.open(); | ||
logger4.info('Info log message'); | ||
logger4.close(); | ||
logger4.on('close', () => { | ||
logger4.removeAllListeners('close'); | ||
logger4.close(); | ||
logger4.on('close', test.mustNotCall()); | ||
logger4.logger.close(); | ||
logger4.logger.on('close', () => { | ||
logger4.logger.removeAllListeners('close'); | ||
logger4.logger.close(); | ||
logger4.logger.on('close', test.mustNotCall()); | ||
test.end(); | ||
@@ -119,4 +119,4 @@ }); | ||
metatests.test('logger.rotate', test => { | ||
logger5.rotate(); | ||
logger5.logger.rotate(); | ||
test.end(); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
15732
346
1