-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: upgrade to support new version of log4
- Rewrite with ES2015 syntax - Rely on log4-nodejs utils - Cover with tests BREAKING CHANGE: - Drop support for log4 v2 - Drop support for Node.js v4 - Switch formatter to rely on log4-nodejs utils - Change presentation of prefixes
- Loading branch information
Showing
3 changed files
with
87 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,39 @@ | ||
"use strict"; | ||
|
||
if (!process.env.AWS_EXECUTION_ENV) return; // Do not proceed in non AWS lambda env | ||
const d = require("d") | ||
, rootLogger = require("log4") | ||
, emitter = require("log4/writer-utils/emitter") | ||
, registerMaster = require("log4/writer-utils/register-master") | ||
, setupVisibility = require("log4/writer-utils/setup-visibility") | ||
, formatMessage = require("log4-nodejs/utils/format-message") | ||
, levelPrefixes = require("log4-nodejs/utils/level-prefixes"); | ||
|
||
var format = require("util").format | ||
, logger = require("log4") | ||
, setupVisibility = require("log4/setup-visibility"); | ||
const setupPrefixes = levelLogger => { | ||
levelLogger.levelMessagePrefix = levelPrefixes[levelLogger.level]; | ||
Object.defineProperty( | ||
levelLogger, "namespaceMessagePrefix", d.gs(function () { return this.namespace; }) | ||
); | ||
}; | ||
|
||
var conf = Object.create(null); | ||
module.exports = () => { | ||
// Ensure it's the only log4 writer initialzed in a process | ||
registerMaster(); | ||
|
||
// Setup visibility | ||
// Resolve from LOG_* env vars | ||
logger.predefinedLevels.forEach(function (level) { | ||
var varName = "LOG_" + level.toUpperCase(); | ||
if (process.env[varName]) conf[level] = process.env[varName].split(","); | ||
}); | ||
// Eventually support as fallback DEBUG env var | ||
if (!conf.debug && process.env.DEBUG) conf.debug = process.env.DEBUG.split(","); | ||
// Do not show debug level logs by default | ||
if (!conf.debug) conf.debug = []; | ||
conf.debug.unshift("-*"); | ||
setupVisibility(conf); | ||
// Read logs visiblity settings from env variables | ||
setupVisibility( | ||
process.env.LOG_LEVEL, (process.env.LOG_DEBUG || process.env.DEBUG || "").split(",") | ||
); | ||
|
||
// Log | ||
logger.emitter.on("log", function (event) { | ||
var currentLogger = event.logger; | ||
var prefix = "[" + currentLogger.level + "]"; | ||
if (currentLogger.ns) prefix += "[" + currentLogger.ns + "]"; | ||
prefix += " "; | ||
event.messagePrefix = prefix; | ||
if (module.exports.prefixDecorator) prefix = module.exports.prefixDecorator(prefix); | ||
event.messageText = format.apply(null, event.messageTokens); | ||
console.log(prefix + event.messageText); | ||
}); | ||
// Resolve level and namespace log message prefixes | ||
// - for already initialized loggers | ||
rootLogger.getAllInitializedLevels().forEach(setupPrefixes); | ||
// - for loggers to be initialized | ||
emitter.on("init", event => { if (!event.logger.namespace) setupPrefixes(event.logger); }); | ||
|
||
// Write logs to stderr | ||
emitter.on("log", event => { | ||
if (!event.logger.isEnabled) return; | ||
formatMessage(event); | ||
console.error(event.message); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"use strict"; | ||
|
||
const test = require("tape") | ||
, requireUncached = require("cjs-module/require-uncached") | ||
, overrideEnv = require("process-utils/override-env"); | ||
|
||
test("log4-aws-lambda", t => { | ||
const { log, initializeWriter } = overrideEnv(() => | ||
requireUncached( | ||
[ | ||
require.resolve("log4"), require.resolve("log4/writer-utils/emitter"), | ||
require.resolve("log4/writer-utils/register-master"), | ||
require.resolve("log4/writer-utils/setup-visibility"), require.resolve("../") | ||
], | ||
() => ({ log: require("log4"), initializeWriter: require("../") }) | ||
)); | ||
initializeWriter(); | ||
const originalWrite = console.error; | ||
let isInvoked = false; | ||
console.error = string => { | ||
t.equal( | ||
string, | ||
`${ log.error.get("elo").levelMessagePrefix } ${ | ||
log.error.get("elo").namespaceMessagePrefix | ||
} foo bar`, | ||
"Should write logs for enabled loggers to stderr" | ||
); | ||
isInvoked = true; | ||
}; | ||
log("not enabled"); | ||
t.equal(isInvoked, false, "Should not write logs of disabled loggers"); | ||
log.error.get("elo")("foo bar"); | ||
t.equal(isInvoked, true, "Should write logs immediately"); | ||
console.error = originalWrite; | ||
t.end(); | ||
}); |