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 @@

API

  • Log(opts, baseMeta) - Logger class.
  • Log.addMeta(meta)
  • Log.addReq(req)
  • +
  • Log.transformMeta(fn)
  • Log.addRes(res)
  • Log.addError(err)
  • @@ -151,6 +152,15 @@

    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)

    @@ -162,11 +172,15 @@

    Contributions

    build status:

    Build Status

    Licenses

    -

    All code is 2013-2015 Formidable Labs. +

    All code is 2013-2016 Formidable Labs. Released under the MIT License.

    History

    +

    0.1.0

    +

    0.0.6