redis-log
Yet another Node.js logging module. This is a lightweight, opinionated logging library, which is optimized for async/await (ES7) logging with as little boilerplate code as possible.
This library only supports error logging (at least for now).
Downloads
How it works
- Set the
NAME
environment variable to the name of your project (e.g.Awesome Project
). It will be used to group the logs. - Require the module in the starting point of your application and call the
init()
static method (that file should be in the root of your project)
const Log = ;Log; //this should only be called once
- Get an instance of the logger in each file where you're going to use it
const Log = ;const logger = Log;
- Use the logger when calling some async stuff
{ const connection = await pg; const rows = await connection; await connection;} { const connection = await pg; //in case of an exception, this method will still throw the original one, so you should probably still use try/catch //the difference is that it will also intercept and log the error to the Redis database const rows = await logger; await connection;}
Output log example
"timestamp": "18/04/2019 11:27" "message": "Failed to execute A(). Stack:" "stack": "TypeError: Cannot read property 'u' of null\n at A (/Users/patrickpissurno/Documents/GitHub/redis-log/example.js:9:11)\n at B (/Users/patrickpissurno/Documents/GitHub/redis-log/example.js:14:23)\n at main (/Users/patrickpissurno/Documents/GitHub/redis-log/example.js:19:26)\n at Object.<anonymous> (/Users/patrickpissurno/Documents/GitHub/redis-log/example.js:23:1)\n at Module._compile (internal/modules/cjs/loader.js:689:30)\n at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)" "arguments": /* TypeError: Cannot read property 'u' of null at A (/Users/patrickpissurno/Documents/GitHub/redis-log/example.js:9:11) at B (/Users/patrickpissurno/Documents/GitHub/redis-log/example.js:14:23) at main (/Users/patrickpissurno/Documents/GitHub/redis-log/example.js:19:26) at Object.<anonymous> (/Users/patrickpissurno/Documents/GitHub/redis-log/example.js:23:1) at Module._compile (internal/modules/cjs/loader.js:689:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)*/
Complete usage example
This is how some production code might look like
const pg = pg; //not required, just for example's sake const Log = ;const logger = Log; //init should only be called once { const connection = await pg; try const rows = await connection; console; catchex throw ex; finally await connection; } logger;
Available parameters (API Reference)
Log.init(dirname)
dirname
: Required. Should be the content of the resolved path for the root folder of the application (usually__dirname
of the starting point of the app)
Log.getLogger(filename, opts)
filename
: Required. Should be the content of the__filename
variable for the current module fileopts
: Optional. This object is passed down to the Redis instance (ioredis), so you can configure things like host, port, and all the other options available at their docs
logger.try(asyncFunction, functionArguments, extraMessage, shouldThrow)
asyncFunction
: Required. A function that returns aPromise
functionArguments
: Optional. An array containing the arguments you want that function to receiveextraMessage
: Optional. A string containing some extra information you want to be stored together with the log, such as some sort of label. For example: "myMethod() #1 crashed"shouldThrow
: Optional. Default: true. Whether the function should re-throw the exception in case the Promise throws
Sponsors
License
MIT License
Copyright (c) 2019 Patrick Pissurno
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.