Skip to content
This repository has been archived by the owner on Dec 13, 2018. It is now read-only.

Logging

Peter Elmers edited this page Aug 30, 2018 · 4 revisions

Use the logging package to write debug output.

The current log file can be found at:

ls `node -p 'require("os").tmpdir()'`/nuclide-${USER}-logs/nuclide.log

Introduction

We avoid writing logs with console.log because it pollutes the JavaScript console. It's fine to use it for local development, but you should remove the calls before committing your code.

The logging package uses the log4js-node loggers, so most of their documentation is applicable.

The logging’s README provides more details.

Log file location

By default, the logs are written to /tmp/nuclide-$USER-logs/nuclide.log-YYYY-MM-DD. The log directory can be customized in the Nuclide Settings.

Usage

Get an instance of the default logger

var logger = require('logging').getLogger();

Call the appropriate logging function, based on severity

See the log4js-node documentation for more details. Here's a quick sample:

logger.trace('Entering cheese testing');
logger.debug('Got cheese.');
logger.info('Cheese is Gouda.');
logger.warn('Cheese is quite smelly.');
logger.error('Cheese is too ripe!');
logger.fatal('Cheese was breeding ground for listeria.');

If you must use the console

Most people assume that the console.log() method (and associated debug, info, warn, and error methods) takes a string as its only argument and prints it. This is not true: its API is much more powerful than that. Peruse the docs to learn about the full power of the console object], or at least brush up on the most common use cases here.

For example, console.log() can take a format string as its first argument:

console.log('My name is %s.', name);

The first argument can also be an object rather than a string:

console.log({'key': 'value'});

Though for objects, console.dir() is often preferred because it makes it possible explore the object:

console.dir({'key': 'value'});

Though for maximum win, use %o in your format string, as this prints your message and enables you to explore your object:

console.log('The response was: %o', {'key': 'value'});