diff --git a/HISTORY.md b/HISTORY.md index d922e15..c8ad724 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,6 +1,10 @@ History ======= +## 0.1.0 + +* Add `transformMeta()` function. ([@zianwar][]) + ## 0.0.6 * Permissively infer `error`, `warn|warning` levels for request logger. ([@slooker][]) @@ -34,3 +38,4 @@ History [@ryan-roemer]: https://github.com/ryan-roemer [@didebeach]: https://github.com/didebeach [@slooker]: https://github.com/slooker +[@zianwar]: https://github.com/zianwar diff --git a/LICENSE.txt b/LICENSE.txt index 9d782e2..0e1c1e7 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,4 +1,4 @@ -Copyright (C) 2013-2015 Formidable Labs, Inc. +Copyright (C) 2013-2016 Formidable Labs, Inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 38004da..0eae1f1 100644 --- a/README.md +++ b/README.md @@ -128,6 +128,7 @@ app.get("/foo", function (req, res) { * [`Log(opts, baseMeta)` - Logger class.](#-log-opts-basemeta-logger-class-) * [`Log.addMeta(meta)`](#-log-addmeta-meta-) * [`Log.addReq(req)`](#-log-addreq-req-) +* [`Log.transformMeta(fn)`](#-log-transformmeta-fn-) * [`Log.addRes(res)`](#-log-addres-res-) * [`Log.addError(err)`](#-log-adderror-err-) @@ -185,6 +186,19 @@ Add arbitrary meta to all subsequent log statements. Add request to meta. +### `Log.transformMeta(fn)` + +Set a delayed single transform function to mutate a **copy** of the metadata +_right before_ a logging event. You can only presently have **one** such +function. And it is delayed so that for things like request end, you can +effectively access **all** the metadata. + +The transform is applied on each log call and passes a copy of the mutated +metadata to the actual log call. + +The function signature should be `fn(existingMeta)` and return mutated +metadata. + ### `Log.addRes(res)` Add response to meta. @@ -208,7 +222,7 @@ We test all changes with [Travis CI][trav]. Here's our current [trav_site]: https://travis-ci.org/FormidableLabs/express-winston-middleware ## Licenses -All code is 2013-2015 Formidable Labs. +All code is 2013-2016 Formidable Labs. Released under the [MIT](./LICENSE.txt) License. [winston]: https://github.com/flatiron/winston diff --git a/examples/server.js b/examples/server.js index 1a85d90..80371f3 100755 --- a/examples/server.js +++ b/examples/server.js @@ -35,6 +35,33 @@ app.get("/custom-logging", function (req, res) { }); res.send("Custom message logged..."); }); +app.get("/add-meta", function (req, res) { + res.locals._log + .addMeta({ + extra: "addMeta-meta" + }) + .info("This is the per-request logger object!"); + + res.send("Custom add-meta logged..."); +}); +app.get("/transform-meta", function (req, res) { + var log = res.locals._log + .transformMeta(function (meta) { + return { + oldReq: meta.req, + totallyNew: "totally new stuff" + }; + }); + + log.info("This is the per-request logger object!"); + log.warn("This is extra custom log with callback", function () { + // Do a console log so we don't have our logger everywhere... + console.log("CONSOLE: CALLED BACK"); + }); + + res.send("Custom transform-meta logged..."); +}); + app.get("/error", function (req, res, next) { next(new Error("Error!")); }); diff --git a/index.html b/index.html index f73d1df..a51c6a8 100644 --- a/index.html +++ b/index.html @@ -120,6 +120,7 @@
Log(opts, baseMeta)
- Logger class.Log.addMeta(meta)
Log.addReq(req)
Log.transformMeta(fn)
Log.addRes(res)
Log.addError(err)
Log.addMeta(meta)
Add arbitrary meta to all subsequent log statements.
Log.addReq(req)
Add request to meta.
+Log.transformMeta(fn)
Set a delayed single transform function to mutate a copy of the metadata +right before a logging event. You can only presently have one such +function. And it is delayed so that for things like request end, you can +effectively access all the metadata.
+The transform is applied on each log call and passes a copy of the mutated +metadata to the actual log call.
+The function signature should be fn(existingMeta)
and return mutated
+metadata.
Log.addRes(res)
Add response to meta.
Log.addError(err)
All code is 2013-2015 Formidable Labs. +
All code is 2013-2016 Formidable Labs. Released under the MIT License.
transformMeta()
function. (@zianwar)error
, warn|warning
levels for request logger. (@slooker)
diff --git a/index.js b/index.js
index b54aa85..33388d9 100644
--- a/index.js
+++ b/index.js
@@ -268,17 +268,25 @@ Log = function (opts, baseMeta) {
}
}, baseMeta);
+ // Passthrough transform function.
+ this._metaTransformFn = null;
+
// Iterate and patch all log levels.
_.each(this.levels, function (num, level) {
self[level] = function (msg, metaOrCb, callback) {
var meta = _.extend({ date: (new Date()).toISOString(), }, this._meta),
- args = [msg, meta];
+ args;
// Extend with user-passed meta, if applicable.
if (_.isObject(metaOrCb)) {
_.extend(meta, metaOrCb, {});
}
+ // Apply final transform, if any.
+ if (self._metaTransformFn) {
+ meta = self._metaTransformFn(_.cloneDeep(meta));
+ }
+
// Infer arguments per Winston calling conventions.
if (arguments.length === 2 && _.isFunction(metaOrCb)) {
// Push callback to end.
@@ -287,6 +295,10 @@ Log = function (opts, baseMeta) {
} else if (arguments.length > 2) {
// In order already.
args = [msg, meta, callback];
+
+ } else {
+ // No callback.
+ args = [msg, meta];
}
// Call real logger.
@@ -339,13 +351,22 @@ Log.prototype.addReq = function (req) {
/**
* `Log.transformMeta(fn)`
*
- * Transform meta.
+ * Set a delayed single transform function to mutate a **copy** of the metadata
+ * _right before_ a logging event. You can only presently have **one** such
+ * function. And it is delayed so that for things like request end, you can
+ * effectively access **all** the metadata.
+ *
+ * The transform is applied on each log call and passes a copy of the mutated
+ * metadata to the actual log call.
+ *
+ * The function signature should be `fn(existingMeta)` and return mutated
+ * metadata.
*
* @param {Function} fn Transform function.
* @api public
*/
Log.prototype.transformMeta = function (fn) {
- this._meta = fn(this._meta);
+ this._metaTransformFn = fn;
return this;
};
diff --git a/test/log.spec.js b/test/log.spec.js
index 937655f..91bfb13 100644
--- a/test/log.spec.js
+++ b/test/log.spec.js
@@ -66,6 +66,10 @@ describe("Log", function () {
it("should add tests here"); // TODO
});
+ describe("#transformMeta", function () {
+ it("should add tests here"); // TODO
+ });
+
describe("#addReq", function () {
it("should add tests here"); // TODO
});