- [Instructor] As mentioned console log is synchronous
and not recommended for production.
Additionally it does not let us use different log
levels depending on the environment we are in.
If we look at the warnings
we also see that it complains
about the usage of console log.
We will use bunyan, a dedicated logging module
to implement production grade
logging for our application now.
We will also make the logging use different log levels
depending on the environment we are running in.
To do that, we will now extend our config module a little.
So I'm going into server, config.
Index js, and first I have to install Bunyan.
So I go ahead and run npm install, dash, dash save, bunyan.
and of course I have to require so add const
bunyan equals require bunyan.
Next, let's prepare some configuration
depending on the environment.
So somewhere before the module exports starts
I will now add const loggers equals.
So we need a logger for development.
And this should be a function,
because we want to lazy load those and
instantiate all of them.
So add bunyan dot create logger and I want to
pass in the name, and in this case it's just
the environment we are running and I want to
add the log level.
And, for development, it makes sense to use debug.
Let's duplicate those lines and let's add production.
And test.
And for production of course the name should
be production and the log level in production
environment should be information and test.
The only one to show fatal errors because otherwise
it would just clutter our testing results.
Next, I will simply go ahead into the respective
environment configurations and add a property log.
And for development, it should be loggers dot
development and let's copy this line and go ahead
to production, it should be loggers production.
And, for test it should be loggers dot test.
Now we want to of course use this logger.
So I'm going into bin www and there we already
have the config available obviously and now
I will simply bring in the log from the config
by adding const log equals config dot log.
Don't forget the parentheses here because we are
executing a function.
Now let's look for console log statements
and I will simply search and replace the
now console log with log dot info.
And additionally I want to replace the console error
messages and they should be log dot fatal.
Now let's start the application to see if
everything still works.
And we see that we get now output from
bunyan and the output from bunyan is by
default jason, maybe we want it to be a little bit
more human friendly so I go into package Jason
and there I now just add to the start script.
Hypen and bunyan because bunyan has a console
formatter we will and restart now.
We see that we get now a formatted output
on the console.
As the logger is now part of our config
it's easy to pass it around in our application
wherever we need it.