-
-
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: reconfigure on top of LogWriter
- Loading branch information
Showing
2 changed files
with
17 additions
and
40 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,39 +1,15 @@ | ||
"use strict"; | ||
|
||
const d = require("d") | ||
, rootLogger = require("log") | ||
, emitter = require("log/lib/emitter") | ||
, registerMaster = require("log/lib/register-master") | ||
, setupVisibility = require("log/lib/setup-visibility") | ||
, formatMessage = require("log-node/lib/format-event-message") | ||
, levelPrefixes = require("log-node/lib/level-prefixes"); | ||
|
||
const setupPrefixes = levelLogger => { | ||
levelLogger.levelMessagePrefix = levelPrefixes[levelLogger.level]; | ||
Object.defineProperty( | ||
levelLogger, "namespaceMessagePrefix", d.gs(function () { return this.namespace; }) | ||
); | ||
}; | ||
|
||
module.exports = () => { | ||
// Ensure it's the only log writer initialzed in a process | ||
registerMaster(); | ||
|
||
// Read logs visiblity settings from env variables | ||
setupVisibility( | ||
process.env.LOG_LEVEL, (process.env.LOG_DEBUG || process.env.DEBUG || "").split(",") | ||
); | ||
|
||
// 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); | ||
}); | ||
}; | ||
const formatParts = require("sprintf-kit/format-parts") | ||
, LogWriter = require("log/lib/writer") | ||
, resolveParts = require("log-node/lib/resolve-format-parts"); | ||
|
||
class AwsLambdaLogWriter extends LogWriter { | ||
constructor(options = {}) { super(process.env, options); } | ||
resolveMessageContent(event) { | ||
event.messageContent = formatParts(resolveParts(...event.messageTokens)); | ||
} | ||
writeMessage(event) { console.error(event.message); } | ||
} | ||
|
||
module.exports = (options = {}) => new AwsLambdaLogWriter(options); |
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