DEV Community

Jan Dvorak
Jan Dvorak

Posted on • Updated on

Meteor logging facility

In my previous article in the series I mentioned the Meteor logging facility package logging. Sadly this package has no documentation, as it was intended for internal usage, so I had to dig into the code to figure out how it works and what it offers. Like any other logging tool it offers the necessary tools for writing your messages to the console. Now in what it differs is that it will also write the filename and line where it was called.

Getting started

As with any other Meteor package you add it from Atmosphere:

meteor add logging
Enter fullscreen mode Exit fullscreen mode

Import is as follows:

import { Log } from 'meteor/logging'
Enter fullscreen mode Exit fullscreen mode

Usage

For basic logging you can use it as follows:

Log('starting up') // or Log.info('starting up')
Log.error('error test')
Log.warn('debug test')
Enter fullscreen mode Exit fullscreen mode

This will result in the usual the usual color coded output that you are used to when using the equivalent console call. Works both on client and server.

Now where this shines is that you can pass in an object which will then be handled provided you don't pass in any of the restricted params:

['time', 'timeInexact', 'level', 'file', 'line', 'program', 'originApp', 'satellite', 'stderr']
Enter fullscreen mode Exit fullscreen mode

That is because those will be added automatically and filled with the proper values for you.

Now let's take a look what we can do here:

Log.info({property1: 'foo', property2: 'bar', property3: { foo: 'bar' }})
Log.info({message: 'warning', error: { property1: 'foo', property2: 'bar', property3: { foo: 'bar' }} })
Enter fullscreen mode Exit fullscreen mode

will turn into:

E20200519-17:45:28.038(9) (main.js:35) {"property1":"foo","property2":"bar","property3":{"foo":"bar"}}
E20200519-17:45:28.038(9) (main.js:36) warning {"error":{"property1":"foo","property2":"bar","property3":{"foo":"bar"}}}
Enter fullscreen mode Exit fullscreen mode

Now notice here that message is a reserved param that is then separated out of the object to provide you info before it.

Another param that you can add is app:

Log.info({message: 'warning', app: 'LU', error: { property1: 'foo', property2: 'bar', property3: { foo: 'bar' }} })
Enter fullscreen mode Exit fullscreen mode
E20200519-17:57:41.655(9) [LU] (main.js:36) warning {"error":{"property1":"foo","property2":"bar","property3":{"foo":"bar"}}}
Enter fullscreen mode Exit fullscreen mode

Inaccessible, but noteworthy

Now then, from the restricted params above you can guess that there is Log.format which parses the object into something more readable.
These is also Log.debug, but that one does not get processed (from the looks of it was meant for packages to have some secret functionality to turn on).

Thoughts

I find logging an interesting package that can already be used in Meteor applications, and I plan to start implementing it in my own apps, even though it is marked as internal.
As can be obvious from this article it would benefit from proper documentation and more functionality. Specifically making Log.debug available, updating of dependencies, hook to send info to external service if you use one and displaying objects across multiple lines like console.dir does. This in order to make it a package available for everyone.
Anyhow I started towards that.


If you like my work, please consider supporting me on GitHub Sponsors ❤️.

Top comments (3)

Collapse
 
olivierjm profile image
Olivier JM Maniraho

Thanks for this,
Does this logger write to a log file by default or this has to be implemented separately?

Collapse
 
storytellercz profile image
Jan Dvorak

The logger currently writes out in the console. Currently you have to implement any third-party or additional functionality separately.
I have a PR to update this package: github.com/meteor/meteor/pull/11068
I'm also planning to add a hook that would allow you to easily plug into the reporting so that you only need to call one log function. There are a few ways of how this can be implemented, but I'm most likely going to use the Meteor hook utility that I recently found (probably for another article) so that it has a familiar setup and use.

Collapse
 
olivierjm profile image
Olivier JM Maniraho

Awesome, thanks a lot for the explanation, I will surely try it out.