Skip to content

Commit

Permalink
feat: upgrade to support new version of log4
Browse files Browse the repository at this point in the history
- 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
medikoo committed Jun 1, 2018
1 parent 6c0ed86 commit ca3fac6
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 33 deletions.
61 changes: 33 additions & 28 deletions index.js
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);
});
};
23 changes: 18 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,24 @@
"url": "git://github.com/medikoo/log4-aws-lambda.git"
},
"dependencies": {
"log4": "1"
"d": "1",
"log4-nodejs": "1"
},
"devDependencies": {
"eslint": "^4.5",
"eslint-config-medikoo-es5": "^1.4.3"
"cjs-module": "^1.3",
"eslint": "^4.19.1",
"eslint-config-medikoo": "^1.35",
"istanbul": "^0.4.5",
"log4": "2",
"process-utils": "^1.1",
"tape": "^4.9",
"tape-index": "2"
},
"peerDependencies": {
"log4": "2"
},
"eslintConfig": {
"extends": "medikoo-es5",
"extends": "medikoo",
"root": true,
"env": {
"node": true
Expand All @@ -35,7 +45,10 @@
}
},
"scripts": {
"lint": "eslint --ignore-path=.gitignore ."
"coverage": "tape-index && istanbul cover test.index.js",
"check-coverage": "npm run coverage && istanbul check-coverage --statements 100 --function 100 --branches 100 --lines 100",
"lint": "eslint --ignore-path=.gitignore .",
"test": "tape-index && node test.index.js"
},
"license": "ISC"
}
36 changes: 36 additions & 0 deletions test/index.js
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();
});

0 comments on commit ca3fac6

Please sign in to comment.