Meteor logging facility
Tags: meteor
Published: 2020-05-19 10:08:22

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:
```bash
meteor add logging
```

Import is as follows:
```js
import { Log } from 'meteor/logging'
```

## Usage

For basic logging you can use it as follows:
```js
Log('starting up') // or Log.info('starting up')
Log.error('error test')
Log.warn('debug test')
```
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:
```js
['time', 'timeInexact', 'level', 'file', 'line', 'program', 'originApp', 'satellite', 'stderr']
```

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:
```js
Log.info({property1: 'foo', property2: 'bar', property3: { foo: 'bar' }})
Log.info({message: 'warning', error: { property1: 'foo', property2: 'bar', property3: { foo: 'bar' }} })
```
will turn into:
```bash
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"}}}
```
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`:
```js
Log.info({message: 'warning', app: 'LU', error: { property1: 'foo', property2: 'bar', property3: { foo: 'bar' }} })
```
```bash
E20200519-17:57:41.655(9) [LU] (main.js:36) warning {"error":{"property1":"foo","property2":"bar","property3":{"foo":"bar"}}}
```

### 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](https://github.com/meteor/meteor/pull/11068) towards that.

--------
If you like my work, please consider supporting me on [GitHub Sponsors ❤️](https://github.com/sponsors/StorytellerCZ).