Comparing version 0.1.0 to 0.2.0
@@ -6,2 +6,12 @@ 'use strict' | ||
const Transport = require('./Transport') | ||
const ConsoleTransport = require('./ConsoleTransport') | ||
const { | ||
addMethodsForLevelsSym, | ||
levelsArraySym, | ||
levelStringSym, | ||
levelSym, | ||
levelsSym, | ||
transportsSym, | ||
preparedMetasSym | ||
} = require('./Symbols') | ||
@@ -18,18 +28,30 @@ const defaultLevels = { | ||
function createLogger (options = {}) { | ||
const transport = new Transport({ | ||
stream: options.stream || process.stdout, | ||
format: options.format || new SimpleFormat() | ||
}) | ||
return new Logger({ | ||
transports: [ transport ], | ||
levels: options.levels || defaultLevels, | ||
level: options.level | ||
}) | ||
const levels = options.levels || defaultLevels | ||
const levelString = options.level || 'INFO' | ||
const logger = new Logger() | ||
logger[transportsSym] = options.transports || [ getTransport(options) ] | ||
logger[levelsSym] = levels | ||
logger[levelStringSym] = levelString | ||
logger[levelSym] = levels[levelString] | ||
logger[levelsArraySym] = Object.keys(levels) | ||
logger[addMethodsForLevelsSym]() | ||
logger[preparedMetasSym] = [] | ||
return logger | ||
} | ||
function getTransport (options) { | ||
const format = options.format || new SimpleFormat() | ||
if (!options.stream) { | ||
return new ConsoleTransport({ format }) | ||
} else { | ||
const stream = options.stream | ||
return new Transport({ stream, format }) | ||
} | ||
} | ||
module.exports = { | ||
createLogger, | ||
Transport, | ||
Logger, | ||
SimpleFormat | ||
SimpleFormat, | ||
ConsoleTransport | ||
} |
'use strict' | ||
const eachAsync = require('async-each') | ||
const { | ||
addMethodsForLevelsSym, | ||
levelsArraySym, | ||
levelStringSym, | ||
levelSym, | ||
levelsSym, | ||
transportsSym, | ||
preparedMetasSym, | ||
prepareMetaSym | ||
} = require('./Symbols') | ||
const addMethodsForLevels = Symbol('apheleia.addMethodsForLevels') | ||
const transportsSym = Symbol('apheleia.transports') | ||
const levelsSym = Symbol('apheleia.levels') | ||
const levelSym = Symbol('apheleia.level') | ||
const preparedMetasSym = Symbol('apheleia.preparedMeta') | ||
const prepareMetaSym = Symbol('apheleia.prepareMeta') | ||
module.exports = class Logger { | ||
constructor (options) { | ||
this[transportsSym] = options.transports | ||
this[levelsSym] = options.levels | ||
this[levelSym] = this[levelsSym][options.level || 'INFO'] | ||
this[addMethodsForLevels]() | ||
this[preparedMetasSym] = [] | ||
} | ||
child (meta) { | ||
const logger = new Logger({ | ||
transports: this[transportsSym], | ||
levels: this[levelsSym], | ||
level: Object.keys(this[levelsSym])[this[levelSym]] | ||
}) | ||
const logger = new Logger() | ||
logger[transportsSym] = this[transportsSym] | ||
logger[levelsSym] = this[levelsSym] | ||
logger[levelStringSym] = this[levelStringSym] | ||
logger[levelSym] = this[levelSym] | ||
logger[levelsArraySym] = this[levelsArraySym] | ||
logger[addMethodsForLevelsSym]() | ||
logger[prepareMetaSym](meta) | ||
logger.end = function end (callback) { | ||
callback() | ||
} | ||
logger.end = end | ||
return logger | ||
@@ -58,4 +53,4 @@ } | ||
[addMethodsForLevels] () { | ||
for (const level of Object.keys(this[levelsSym])) { | ||
[addMethodsForLevelsSym] () { | ||
for (const level of this[levelsArraySym]) { | ||
this[level.toLowerCase()] = (message, ...args) => { | ||
@@ -67,1 +62,5 @@ this.log(level, message, ...args) | ||
} | ||
function end (callback) { | ||
callback() | ||
} |
@@ -8,3 +8,3 @@ 'use strict' | ||
this.dateFormatter = new DateFormat({ | ||
dateFormat: options.dateFormat || 'YYYY-MM-DDTHH:mm:ss.sss[Z]', | ||
dateFormat: options.dateFormat || 'YYYY-MM-DDTHH:mm:ss.SSS[Z]', | ||
cache: true | ||
@@ -25,3 +25,3 @@ }) | ||
} | ||
return `${level} ${date} ${message}${line}${meta}${stack}` | ||
return `${date} ${level} ${message}${line}${meta}${stack}` | ||
} | ||
@@ -28,0 +28,0 @@ |
'use strict' | ||
const streamSym = Symbol('apheleia.stream') | ||
const formatSym = Symbol('apheleia.format') | ||
const { | ||
formatSym, | ||
streamSym | ||
} = require('./Symbols') | ||
@@ -6,0 +8,0 @@ module.exports = class Transport { |
{ | ||
"name": "apheleia", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "A simplistic logger.", | ||
@@ -18,9 +18,13 @@ "main": "lib/Apheleia.js", | ||
"async-each": "^1.0.1", | ||
"fast-date-format": "^2.0.0" | ||
"fast-date-format": "^2.1.0" | ||
}, | ||
"devDependencies": { | ||
"@clevernature/benchmark-regression": "^1.0.1", | ||
"benchmark": "^2.1.4", | ||
"coveralls": "^3.0.2", | ||
"daily-rotating-file-stream": "^1.0.0", | ||
"fast-file-rotate": "^1.0.1", | ||
"standard": "^12.0.1", | ||
"tap": "^12.1.2" | ||
"tap": "^12.1.2", | ||
"winston": "^3.1.0" | ||
}, | ||
@@ -32,3 +36,7 @@ "keywords": [ | ||
"stream" | ||
] | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/SerayaEryn/apheleia.git" | ||
} | ||
} |
@@ -32,3 +32,3 @@ # apheleia | ||
#### stream (optional) | ||
A Writable stream. Defaults to `process.stdout`. | ||
A Writable stream. If undefined a `ConsoleTransport` will be used. | ||
@@ -51,64 +51,25 @@ #### format (optional) | ||
### SimpleFormat([options]) | ||
#### transports (optional) | ||
### Logger(options) | ||
Allows to set the transport of the logger. | ||
Creates a new Logger instance. | ||
### SimpleFormat([options]) | ||
```js | ||
new Logger({ | ||
transports: [ | ||
new Transport({ | ||
stream: process.stdout, | ||
format: new SimpleFormat() | ||
} | ||
], | ||
levels: { | ||
TRACE: 0, | ||
DEBUG: 1, | ||
INFO: 2, | ||
WARN: 3, | ||
ERROR: 4, | ||
FATAL: 5 | ||
}, | ||
level: 'DEBUG' | ||
}) | ||
``` | ||
### Logger | ||
#### transports | ||
#### Logger#end(callback) | ||
An array of transports. | ||
#### levels (optional) | ||
The levels used by the logger. | ||
Defaults to : | ||
```js | ||
{ | ||
TRACE: 0, | ||
DEBUG: 1, | ||
INFO: 2, | ||
WARN: 3, | ||
ERROR: 4, | ||
FATAL: 5 | ||
} | ||
``` | ||
#### level (optional) | ||
Sets the log level of the logger. | ||
### Logger#end(callback) | ||
Ends the logger by calling `end()` on every transport and then calls the `callback`. | ||
### Logger#trace(message, ...args) | ||
#### Logger#trace(message, ...args) | ||
Writes a `TRACE` level log, if allowed by the `level` option. | ||
### Logger#debug(message, ...args) | ||
#### Logger#debug(message, ...args) | ||
Writes a `DEBUG` level log, if allowed by the `level` option. | ||
### Logger#info(message, ...args) | ||
#### Logger#info(message, ...args) | ||
Writes a `INFO` level log, if allowed by the `level` option. | ||
### Logger#warn(message, ...args) | ||
#### Logger#warn(message, ...args) | ||
Writes a `WARN` level log, if allowed by the `level` option. | ||
### Logger#error(message, ...args) | ||
#### Logger#error(message, ...args) | ||
Writes a `ERROR` level log, if allowed by the `level` option. | ||
### Logger#fatal(message, ...args) | ||
#### Logger#fatal(message, ...args) | ||
Writes a `FATAL` level log, if allowed by the `level` option. | ||
@@ -133,4 +94,10 @@ | ||
### ConsoleTransport(options) | ||
#### format | ||
A format used to stringify the parameters passed to the loggers methods. | ||
## License | ||
[MIT](./LICENSE) |
@@ -51,3 +51,3 @@ 'use strict' | ||
const buffer = fs.readFileSync(fileName) | ||
t.ok(buffer.toString().startsWith('INFO')) | ||
t.ok(buffer.toString().includes('INFO')) | ||
t.ok(buffer.toString().endsWith('test test test\n')) | ||
@@ -57,2 +57,17 @@ }) | ||
test('should log with level info', (t) => { | ||
t.plan(2) | ||
const fileName = getFile() | ||
const logger = createLogger({ | ||
stream: new RotatingStream({ fileName }) | ||
}) | ||
logger.info('test test test', 'something', { test: 42 }, () => {}) | ||
logger.end(() => { | ||
const buffer = fs.readFileSync(fileName) | ||
t.ok(buffer.toString().includes('INFO')) | ||
t.ok(buffer.toString().endsWith('test test test something\n{"test":42}\n')) | ||
}) | ||
}) | ||
test('should log with level warn', (t) => { | ||
@@ -68,3 +83,3 @@ t.plan(2) | ||
const buffer = fs.readFileSync(fileName) | ||
t.ok(buffer.toString().startsWith('WARN')) | ||
t.ok(buffer.toString().includes('WARN')) | ||
t.ok(buffer.toString().endsWith('test test test\n')) | ||
@@ -84,3 +99,3 @@ }) | ||
const buffer = fs.readFileSync(fileName) | ||
t.ok(buffer.toString().startsWith('ERROR')) | ||
t.ok(buffer.toString().includes('ERROR')) | ||
t.ok(buffer.toString().endsWith('test test test\n')) | ||
@@ -100,3 +115,3 @@ }) | ||
const log = fs.readFileSync(fileName).toString() | ||
t.ok(log.startsWith('ERROR')) | ||
t.ok(log.includes('ERROR')) | ||
t.ok(log.includes('test test test\nError: booom\n')) | ||
@@ -118,3 +133,3 @@ }) | ||
const log = fs.readFileSync(fileName).toString() | ||
t.ok(log.startsWith('ERROR')) | ||
t.ok(log.includes('ERROR')) | ||
t.ok(log.includes('test test test test=42\nError: booom\n')) | ||
@@ -135,3 +150,3 @@ }) | ||
const log = fs.readFileSync(fileName).toString() | ||
t.ok(log.startsWith('INFO')) | ||
t.ok(log.includes('INFO')) | ||
t.ok(log.endsWith('test test test test=42\n')) | ||
@@ -154,3 +169,3 @@ }) | ||
const log = fs.readFileSync(fileName).toString() | ||
t.ok(log.startsWith('INFO')) | ||
t.ok(log.includes('INFO')) | ||
t.ok(log.endsWith('test test test\n')) | ||
@@ -160,2 +175,144 @@ }) | ||
}) | ||
test('should log with level trace to stdout', (t) => { | ||
t.plan(3) | ||
const fileName = getFile() | ||
const logger = createLogger({ | ||
level: 'TRACE' | ||
}) | ||
const originalWrite = process.stdout.write | ||
const stream = fs.createWriteStream(fileName) | ||
process.stdout.write = stream.write.bind(stream) | ||
logger.trace('test test test') | ||
stream.on('finish', () => { | ||
logger.end(() => { | ||
process.stdout.write = originalWrite | ||
t.pass('end cb called') | ||
const log = fs.readFileSync(fileName).toString() | ||
t.ok(log.includes('TRACE')) | ||
t.ok(log.endsWith('test test test\n')) | ||
}) | ||
}) | ||
stream.end() | ||
}) | ||
test('should log with level debug to stdout', (t) => { | ||
t.plan(3) | ||
const fileName = getFile() | ||
const logger = createLogger({ | ||
level: 'TRACE' | ||
}) | ||
const originalWrite = process.stdout.write | ||
const stream = fs.createWriteStream(fileName) | ||
process.stdout.write = stream.write.bind(stream) | ||
logger.debug('test test test') | ||
stream.on('finish', () => { | ||
logger.end(() => { | ||
process.stdout.write = originalWrite | ||
t.pass('end cb called') | ||
const log = fs.readFileSync(fileName).toString() | ||
t.ok(log.includes('DEBUG')) | ||
t.ok(log.endsWith('test test test\n')) | ||
}) | ||
}) | ||
stream.end() | ||
}) | ||
test('should log with level info to stdout', (t) => { | ||
t.plan(3) | ||
const fileName = getFile() | ||
const logger = createLogger({ | ||
level: 'TRACE' | ||
}) | ||
const originalWrite = process.stdout.write | ||
const stream = fs.createWriteStream(fileName) | ||
process.stdout.write = stream.write.bind(stream) | ||
logger.info('test test test') | ||
stream.on('finish', () => { | ||
logger.end(() => { | ||
process.stdout.write = originalWrite | ||
t.pass('end cb called') | ||
const log = fs.readFileSync(fileName).toString() | ||
t.ok(log.includes('INFO')) | ||
t.ok(log.endsWith('test test test\n')) | ||
}) | ||
}) | ||
stream.end() | ||
}) | ||
test('should log with level warn to stderr', (t) => { | ||
t.plan(3) | ||
const fileName = getFile() | ||
const logger = createLogger({}) | ||
const originalWrite = process.stderr.write | ||
const stream = fs.createWriteStream(fileName) | ||
process.stderr.write = stream.write.bind(stream) | ||
logger.warn('test test test') | ||
stream.on('finish', () => { | ||
logger.end(() => { | ||
process.stderr.write = originalWrite | ||
t.pass('end cb called') | ||
const log = fs.readFileSync(fileName).toString() | ||
t.ok(log.includes('WARN')) | ||
t.ok(log.endsWith('test test test\n')) | ||
}) | ||
}) | ||
stream.end() | ||
}) | ||
test('should log with level error to stderr', (t) => { | ||
t.plan(3) | ||
const fileName = getFile() | ||
const logger = createLogger({ | ||
level: 'TRACE' | ||
}) | ||
const originalWrite = process.stderr.write | ||
const stream = fs.createWriteStream(fileName) | ||
process.stderr.write = stream.write.bind(stream) | ||
logger.error('test test test') | ||
stream.on('finish', () => { | ||
logger.end(() => { | ||
process.stderr.write = originalWrite | ||
t.pass('end cb called') | ||
const log = fs.readFileSync(fileName).toString() | ||
t.ok(log.includes('ERROR')) | ||
t.ok(log.endsWith('test test test\n')) | ||
}) | ||
}) | ||
stream.end() | ||
}) | ||
test('should log with level fatal to stderr', (t) => { | ||
t.plan(3) | ||
const fileName = getFile() | ||
const logger = createLogger({ | ||
level: 'TRACE' | ||
}) | ||
const originalWrite = process.stderr.write | ||
const stream = fs.createWriteStream(fileName) | ||
process.stderr.write = stream.write.bind(stream) | ||
logger.fatal('test test test') | ||
stream.on('finish', () => { | ||
logger.end(() => { | ||
process.stderr.write = originalWrite | ||
t.pass('end cb called') | ||
const log = fs.readFileSync(fileName).toString() | ||
t.ok(log.includes('FATAL')) | ||
t.ok(log.endsWith('test test test\n')) | ||
}) | ||
}) | ||
stream.end() | ||
}) | ||
} | ||
@@ -162,0 +319,0 @@ |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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 repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
22960
15
661
8
100
3
1
Updatedfast-date-format@^2.1.0