Comparing version 6.3.0 to 6.4.0
@@ -6,9 +6,25 @@ const streams = require('streamroller'); | ||
function openTheStream(filename, pattern, options) { | ||
const stream = new streams.DateRollingFileStream( | ||
filename, | ||
pattern, | ||
options | ||
); | ||
stream.on('error', (err) => { | ||
console.error('log4js.dateFileAppender - Writing to file %s, error happened ', filename, err); //eslint-disable-line | ||
}); | ||
stream.on("drain", () => { | ||
process.emit("log4js:pause", false); | ||
}); | ||
return stream; | ||
} | ||
/** | ||
* File appender that rolls files according to a date pattern. | ||
* @filename base filename. | ||
* @pattern the format that will be added to the end of filename when rolling, | ||
* @param filename base filename. | ||
* @param pattern the format that will be added to the end of filename when rolling, | ||
* also used to check when to roll files - defaults to '.yyyy-MM-dd' | ||
* @layout layout function for log messages - defaults to basicLayout | ||
* @timezoneOffset optional timezone offset in minutes - defaults to system local | ||
* @param layout layout function for log messages - defaults to basicLayout | ||
* @param options - options to be passed to the underlying stream | ||
* @param timezoneOffset - optional timezone offset in minutes (default system local) | ||
*/ | ||
@@ -26,14 +42,9 @@ function appender( | ||
const logFile = new streams.DateRollingFileStream( | ||
filename, | ||
pattern, | ||
options | ||
); | ||
const writer = openTheStream(filename, pattern, options); | ||
logFile.on("drain", () => { | ||
process.emit("log4js:pause", false); | ||
}); | ||
const app = function (logEvent) { | ||
if (!logFile.write(layout(logEvent, timezoneOffset) + eol, "utf8")) { | ||
if (!writer.writable) { | ||
return; | ||
} | ||
if (!writer.write(layout(logEvent, timezoneOffset) + eol, "utf8")) { | ||
process.emit("log4js:pause", true); | ||
@@ -44,5 +55,3 @@ } | ||
app.shutdown = function (complete) { | ||
logFile.write('', 'utf-8', () => { | ||
logFile.end(complete); | ||
}); | ||
writer.end('', 'utf-8', complete); | ||
}; | ||
@@ -55,3 +64,2 @@ | ||
let layout = layouts.basicLayout; | ||
if (config.layout) { | ||
@@ -65,2 +73,5 @@ layout = layouts.layout(config.layout.type, config.layout); | ||
// security default (instead of relying on streamroller default) | ||
config.mode = config.mode || 0o600; | ||
return appender( | ||
@@ -67,0 +78,0 @@ config.filename, |
@@ -8,2 +8,10 @@ const debug = require('debug')('log4js:file'); | ||
let mainSighupListenerStarted = false; | ||
const sighupListeners = new Set(); | ||
function mainSighupHandler() { | ||
sighupListeners.forEach((app) => { | ||
app.sighupHandler(); | ||
}); | ||
} | ||
function openTheStream(file, fileSize, numFiles, options) { | ||
@@ -41,5 +49,3 @@ const stream = new streams.RollingFileStream( | ||
file = path.normalize(file); | ||
numBackups = numBackups === undefined ? 5 : numBackups; | ||
// there has to be at least one backup if logSize has been specified | ||
numBackups = numBackups === 0 ? 1 : numBackups; | ||
numBackups = (!numBackups && numBackups !== 0) ? 5 : numBackups; | ||
@@ -58,2 +64,5 @@ debug( | ||
const app = function (loggingEvent) { | ||
if (!writer.writable) { | ||
return; | ||
} | ||
if (options.removeColor === true) { | ||
@@ -63,5 +72,5 @@ // eslint-disable-next-line no-control-regex | ||
loggingEvent.data = loggingEvent.data.map(d => { | ||
if (typeof d === 'string') return d.replace(regex, '') | ||
return d | ||
}) | ||
if (typeof d === 'string') return d.replace(regex, ''); | ||
return d; | ||
}); | ||
} | ||
@@ -83,3 +92,7 @@ if (!writer.write(layout(loggingEvent, timezoneOffset) + eol, "utf8")) { | ||
app.shutdown = function (complete) { | ||
process.removeListener('SIGHUP', app.sighupHandler); | ||
sighupListeners.delete(app); | ||
if (sighupListeners.size === 0 && mainSighupListenerStarted) { | ||
process.removeListener('SIGHUP', mainSighupHandler); | ||
mainSighupListenerStarted = false; | ||
} | ||
writer.end('', 'utf-8', complete); | ||
@@ -91,3 +104,7 @@ }; | ||
// `logSize`. | ||
process.on('SIGHUP', app.sighupHandler); | ||
sighupListeners.add(app); | ||
if (!mainSighupListenerStarted) { | ||
process.on('SIGHUP', mainSighupHandler); | ||
mainSighupListenerStarted = true; | ||
} | ||
@@ -103,2 +120,5 @@ return app; | ||
// security default (instead of relying on streamroller default) | ||
config.mode = config.mode || 0o600; | ||
return fileAppender( | ||
@@ -105,0 +125,0 @@ config.filename, |
@@ -33,3 +33,3 @@ const debug = require('debug')('log4js:fileSync'); | ||
this.size = size; | ||
this.backups = backups || 1; | ||
this.backups = backups; | ||
this.options = options; | ||
@@ -84,3 +84,5 @@ this.currentSize = 0; | ||
debug(`Index of ${fileToRename} is ${idx}`); | ||
if (idx < that.backups) { | ||
if (that.backups === 0) { | ||
fs.truncateSync(filename, 0); | ||
} else if (idx < that.backups) { | ||
// on windows, you can get a EEXIST error if you rename a file to an existing file | ||
@@ -151,5 +153,3 @@ // so, we'll try to delete the file we're renaming to first | ||
file = path.normalize(file); | ||
numBackups = numBackups === undefined ? 5 : numBackups; | ||
// there has to be at least one backup if logSize has been specified | ||
numBackups = numBackups === 0 ? 1 : numBackups; | ||
numBackups = (!numBackups && numBackups !== 0) ? 5 : numBackups; | ||
@@ -198,3 +198,3 @@ function openTheStream(filePath, fileSize, numFiles) { | ||
encoding: config.encoding || 'utf8', | ||
mode: config.mode || 0o644 | ||
mode: config.mode || 0o600 | ||
}; | ||
@@ -201,0 +201,0 @@ |
@@ -20,2 +20,3 @@ const path = require('path'); | ||
coreAppenders.set('fileSync', require('./fileSync')); | ||
coreAppenders.set('tcp', require('./tcp')); | ||
@@ -106,3 +107,6 @@ const appenders = new Map(); | ||
setup({ appenders: { out: { type: 'stdout' } }, categories: { default: { appenders: ['out'], level: 'trace' } } }); | ||
const init = () => { | ||
setup({ appenders: { out: { type: 'stdout' } }, categories: { default: { appenders: ['out'], level: 'trace' } } }); | ||
}; | ||
init(); | ||
@@ -134,1 +138,2 @@ configuration.addListener((config) => { | ||
module.exports = appenders; | ||
module.exports.init = init; |
@@ -124,2 +124,7 @@ | ||
socket.on('close', createSocket); | ||
socket.on('error', (e) => { | ||
debug('connection error', e); | ||
canWrite = false; | ||
emptyBuffer(); | ||
}); | ||
} | ||
@@ -126,0 +131,0 @@ |
@@ -42,3 +42,7 @@ | ||
socket.on('timeout', socket.end.bind(socket)); | ||
// don't bother listening for 'error', 'close' gets called after that anyway | ||
socket.on('error', (e) => { | ||
debug('connection error', e); | ||
canWrite = false; | ||
emptyBuffer(); | ||
}); | ||
socket.on('close', createSocket); | ||
@@ -45,0 +49,0 @@ } |
@@ -164,3 +164,7 @@ const debug = require('debug')('log4js:categories'); | ||
setup({ categories: { default: { appenders: ['out'], level: 'OFF' } } }); | ||
const init = () => { | ||
setup({ categories: { default: { appenders: ['out'], level: 'OFF' } } }); | ||
}; | ||
init(); | ||
configuration.addListener(setup); | ||
@@ -203,3 +207,4 @@ | ||
module.exports = { | ||
module.exports = categories; | ||
module.exports = Object.assign(module.exports, { | ||
appendersForCategory, | ||
@@ -210,2 +215,3 @@ getLevelForCategory, | ||
setEnableCallStackForCategory, | ||
}; | ||
init, | ||
}); |
@@ -32,2 +32,3 @@ /** | ||
const connectLogger = require("./connect-logger"); | ||
const recordingModule = require("./appenders/recording"); | ||
@@ -60,2 +61,7 @@ let enabled = false; | ||
function configure(configurationFileOrObject) { | ||
if (enabled) { | ||
// eslint-disable-next-line no-use-before-define | ||
shutdown(); | ||
} | ||
let configObject = configurationFileOrObject; | ||
@@ -78,2 +84,6 @@ | ||
function recording() { | ||
return recordingModule | ||
} | ||
/** | ||
@@ -93,4 +103,10 @@ * Shutdown all log appenders. This will first disable all writing to appenders | ||
// Clone out to maintain a reference | ||
const appendersToCheck = Array.from(appenders.values()); | ||
// Reset immediately to prevent leaks | ||
appenders.init(); | ||
categories.init(); | ||
// Call each of the shutdown functions in parallel | ||
const appendersToCheck = Array.from(appenders.values()); | ||
const shutdownFunctions = appendersToCheck.reduceRight( | ||
@@ -100,5 +116,9 @@ (accum, next) => (next.shutdown ? accum + 1 : accum), | ||
); | ||
if (shutdownFunctions === 0) { | ||
debug("No appenders with shutdown functions found."); | ||
return cb !== undefined && cb(); | ||
} | ||
let completed = 0; | ||
let error; | ||
debug(`Found ${shutdownFunctions} appenders with shutdown functions.`); | ||
@@ -116,8 +136,2 @@ function complete(err) { | ||
} | ||
if (shutdownFunctions === 0) { | ||
debug("No appenders with shutdown functions found."); | ||
return cb !== undefined && cb(); | ||
} | ||
appendersToCheck.filter(a => a.shutdown).forEach(a => a.shutdown(complete)); | ||
@@ -159,5 +173,6 @@ | ||
levels, | ||
addLayout: layouts.addLayout | ||
addLayout: layouts.addLayout, | ||
recording, | ||
}; | ||
module.exports = log4js; |
@@ -10,2 +10,3 @@ /* eslint no-underscore-dangle:0 */ | ||
const stackReg = /at (?:(.+)\s+\()?(?:(.+?):(\d+)(?::(\d+))?|([^)]+))\)?/; | ||
function defaultParseCallStack(data, skipIdx = 4) { | ||
@@ -72,3 +73,7 @@ const stacklines = data.stack.split("\n").slice(skipIdx); | ||
log(level, ...args) { | ||
const logLevel = levels.getLevel(level, levels.INFO); | ||
let logLevel = levels.getLevel(level); | ||
if (!logLevel) { | ||
this._log(levels.WARN, 'log4js:logger.log: invalid value for log-level as first parameter given: ', level); | ||
logLevel = levels.INFO; | ||
} | ||
if (this.isLevelEnabled(logLevel)) { | ||
@@ -121,7 +126,7 @@ this._log(logLevel, args); | ||
Logger.prototype[`is${isLevelMethod}Enabled`] = function() { | ||
Logger.prototype[`is${isLevelMethod}Enabled`] = function () { | ||
return this.isLevelEnabled(level); | ||
}; | ||
Logger.prototype[levelMethod] = function(...args) { | ||
Logger.prototype[levelMethod] = function (...args) { | ||
this.log(level, ...args); | ||
@@ -128,0 +133,0 @@ }; |
{ | ||
"name": "log4js", | ||
"version": "6.3.0", | ||
"version": "6.4.0", | ||
"description": "Port of Log4js to work with node.", | ||
@@ -32,3 +32,3 @@ "homepage": "https://log4js-node.github.io/log4js-node/", | ||
"pretest": "eslint \"lib/**/*.js\" \"test/**/*.js\"", | ||
"test": "tap \"test/tap/**/*.js\" --cov", | ||
"test": "tap \"test/tap/**/*.js\" --cov --timeout=45", | ||
"typings": "tsc -p types/tsconfig.json", | ||
@@ -42,7 +42,7 @@ "codecov": "tap \"test/tap/**/*.js\" --cov --coverage-report=lcov && codecov" | ||
"dependencies": { | ||
"date-format": "^3.0.0", | ||
"debug": "^4.1.1", | ||
"flatted": "^2.0.1", | ||
"rfdc": "^1.1.4", | ||
"streamroller": "^2.2.4" | ||
"date-format": "^4.0.3", | ||
"debug": "^4.3.3", | ||
"flatted": "^3.2.4", | ||
"rfdc": "^1.3.0", | ||
"streamroller": "^3.0.2" | ||
}, | ||
@@ -52,17 +52,17 @@ "devDependencies": { | ||
"callsites": "^3.1.0", | ||
"codecov": "^3.6.1", | ||
"codecov": "^3.8.3", | ||
"deep-freeze": "0.0.1", | ||
"eslint": "^5.16.0", | ||
"eslint": "^8.7.0", | ||
"eslint-config-airbnb-base": "^13.2.0", | ||
"eslint-config-prettier": "^6.5.0", | ||
"eslint-import-resolver-node": "^0.3.2", | ||
"eslint-plugin-import": "^2.18.2", | ||
"eslint-plugin-prettier": "^3.1.1", | ||
"fs-extra": "^8.1.0", | ||
"husky": "^3.0.9", | ||
"nyc": "^14.1.1", | ||
"prettier": "^1.18.2", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-import-resolver-node": "^0.3.6", | ||
"eslint-plugin-import": "^2.25.4", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"fs-extra": "^10.0.0", | ||
"husky": "^7.0.4", | ||
"nyc": "^15.1.0", | ||
"prettier": "^2.5.1", | ||
"proxyquire": "^2.1.3", | ||
"tap": "^14.10.7", | ||
"typescript": "^3.7.2", | ||
"typescript": "^4.5.4", | ||
"validate-commit-msg": "^2.14.0" | ||
@@ -69,0 +69,0 @@ }, |
@@ -24,2 +24,4 @@ // Type definitions for log4js | ||
export function recording(): Recording; | ||
export const levels: Levels; | ||
@@ -136,3 +138,2 @@ | ||
layout?: Layout; | ||
numBackups?: number; | ||
compress?: boolean; // compress the backups | ||
@@ -156,2 +157,5 @@ // keep the file extension when rotating logs | ||
layout?: Layout; | ||
encoding?: string; | ||
mode?: number; | ||
flags?: string; | ||
} | ||
@@ -180,3 +184,3 @@ | ||
encoding?: string; | ||
// default 0644 | ||
// default 0600 | ||
mode?: number; | ||
@@ -192,3 +196,3 @@ // default ‘a’ | ||
// if this value is greater than zero, then files older than that many days will be deleted during log rolling.(default 0) | ||
daysToKeep?: number; | ||
numBackups?: number; | ||
} | ||
@@ -244,2 +248,19 @@ | ||
/** | ||
* TCP Appender | ||
* | ||
* @see https://log4js-node.github.io/log4js-node/tcp.html | ||
*/ | ||
export interface TCPAppender { | ||
type: 'tcp'; | ||
// defaults to 5000 | ||
port?: number | ||
// defaults to localhost | ||
host?: string | ||
// default to __LOG4JS__ | ||
endMsg?: string | ||
// defaults to a serialized log event | ||
layout?: Layout; | ||
} | ||
export interface CustomAppender { | ||
@@ -251,5 +272,34 @@ type: string | AppenderModule; | ||
export interface AppenderModule { | ||
configure: Function | ||
configure: (config: Config, layouts: LayoutsParam) => AppenderGenerator; | ||
} | ||
export type AppenderGenerator = ( | ||
layout: LayoutFunction, | ||
timezoneOffset?: string | ||
) => AppenderFunction; | ||
export type AppenderFunction = (loggingEvent: LoggingEvent) => void; | ||
// TODO: Actually add types here... | ||
// It's supposed to be the full config element | ||
export type Config = any | ||
export interface LayoutsParam { | ||
basicLayout: LayoutFunction; | ||
messagePassThroughLayout: LayoutFunction; | ||
patternLayout: LayoutFunction; | ||
colouredLayout: LayoutFunction; | ||
coloredLayout: LayoutFunction; | ||
dummyLayout: LayoutFunction; | ||
addLayout: (name: string, serializerGenerator: LayoutFunction) => void; | ||
layout: (name: string, config: PatternToken) => LayoutFunction; | ||
} | ||
export interface PatternToken { | ||
pattern: string; // TODO type this to enforce good pattern... | ||
tokens: { [tokenName: string]: () => any }; | ||
} | ||
export type LayoutFunction = (loggingEvent: LoggingEvent) => string; | ||
export type Appender = CategoryFilterAppender | ||
@@ -267,2 +317,3 @@ | ConsoleAppender | ||
| StandardOutputAppender | ||
| TCPAppender | ||
| CustomAppender; | ||
@@ -281,3 +332,3 @@ | ||
levels: Level[]; | ||
getLevel(level: string): Level; | ||
getLevel(level: Level | string, defaultLevel: Level): Level; | ||
addLevels(customLevels: object): void; | ||
@@ -295,2 +346,10 @@ } | ||
export interface Recording { | ||
configure(loggingEvent: LoggingEvent): void | ||
replay(): LoggingEvent[] | ||
playback(): LoggingEvent[] | ||
reset(): void | ||
erase(): void | ||
} | ||
export class Logger { | ||
@@ -300,5 +359,5 @@ new(dispatch: Function, name: string): Logger; | ||
readonly category: string; | ||
level: string; | ||
level: Level | string; | ||
log(...args: any[]): void; | ||
log(level: Level | string, ...args: any[]): void; | ||
@@ -314,3 +373,3 @@ isLevelEnabled(level?: string): boolean; | ||
_log(level: string, data: any): void; | ||
_log(level: Level, data: any): void; | ||
@@ -317,0 +376,0 @@ addContext(key: string, value: any): void; |
@@ -139,1 +139,23 @@ import * as log4js from './log4js'; | ||
}); | ||
log4js.configure({ | ||
appenders: { rec: { type: 'recording' } }, | ||
categories: { default: { appenders: [ 'rec'], 'level': 'debug' } } | ||
}); | ||
const logger8 = log4js.getLogger(); | ||
logger8.level = 'debug' | ||
logger8.debug('This will go to the recording!') | ||
logger8.debug('Another one') | ||
const recording = log4js.recording() | ||
const loggingEvents = recording.playback() | ||
if (loggingEvents.length !== 2) { | ||
throw new Error(`Expected 2 recorded events, got ${loggingEvents.length}`) | ||
} | ||
if (loggingEvents[1].data[0] !== 'Another one') { | ||
throw new Error(`Expected message 'Another one', got ${loggingEvents[1].data[0]}`) | ||
} | ||
recording.reset() | ||
const loggingEventsPostReset = recording.playback() | ||
if (loggingEventsPostReset.length !== 0) { | ||
throw new Error(`Expected 0 recorded events after reset, got ${loggingEventsPostReset.length}`) | ||
} |
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
2866
102974
31
+ Addeddate-format@4.0.14(transitive)
+ Addedflatted@3.3.1(transitive)
+ Addedstreamroller@3.1.5(transitive)
- Removeddate-format@2.1.03.0.0(transitive)
- Removedflatted@2.0.2(transitive)
- Removedstreamroller@2.2.4(transitive)
Updateddate-format@^4.0.3
Updateddebug@^4.3.3
Updatedflatted@^3.2.4
Updatedrfdc@^1.3.0
Updatedstreamroller@^3.0.2