How to use loglevel

Comprehensive loglevel code examples:

How to use loglevel.getLevel:

620
621
622
623
624
625
626
627
628
629
    phoneTime = timePM;
  }
}

// something has gone wrong if no info was matched, save image for later analysis
if (debugFlag || ((!phoneTime || (phoneTime && !phoneTime.isValid())) && log.getLevel() === log.levels.DEBUG)) {
  log.debug('Phone Time: ', id, value.text);
  if (value.image) {
    value.image.write(debugImagePath);
  }

How to use loglevel.noConflict:

78
79
80
81
82
83
84
85
86
87
For example:

```html
<script src="loglevel.min.js"></script>
<script>
var logging = log.noConflict();

logging.error("still pretty easy");
</script>
```

How to use loglevel.enableAll:

24
25
26
27
28
29
30
31
32
33

### Convenient

* Log output keeps line numbers: most JS logging frameworks call console.log methods through wrapper functions, clobbering your stacktrace and making the extra info many browsers provide useless. We'll have none of that thanks.
* It works with all the standard JavaScript loading systems out of the box (CommonJS, AMD, or just as a global)
* Logging is filtered to "warn" level by default, to keep your live site clean in normal usage (or you can trivially re-enable everything with an initial log.enableAll() call)
* Magically handles situations where console logging is not initially available (IE8/9), and automatically enables logging as soon as it does become available (when developer console is opened)
* Extensible, to add other log redirection, filtering, or formatting functionality, while keeping all the above (except you will clobber your stacktrace, see Plugins below)

## Downloading loglevel

How to use loglevel.levels:

106
107
108
109
110
111
112
113
114
115
* A `log.setLevel(level, [persist])` method.

  This disables all logging below the given level, so that after a log.setLevel("warn") call log.warn("something") or log.error("something") will output messages, but log.info("something") will not.

  This can take either a log level name or 'silent' (which disables everything) in one of a few forms:
  * As a log level from the internal levels list, e.g. log.levels.SILENT ← _for type safety_
  * As a string, like 'error' (case-insensitive) ← _for a reasonable practical balance_
  * As a numeric index from 0 (trace) to 5 (silent) ← _deliciously terse, and more easily programmable (...although, why?)_

  Where possible the log level will be persisted. LocalStorage will be used if available, falling back to cookies if not. If neither is available in the current environment (i.e. in Node), or if you pass `false` as the optional 'persist' second argument, persistence will be skipped.

How to use loglevel.log:

652
653
654
655
656
657
658
659
660
661
<Button
  color="inherit"
  size="small"
  onClick={async () => {
    await verifyContext.undoAll();
    log.log('finished');
  }}
>
  Undo
</Button>

How to use loglevel.setDefaultLevel:

1
2
3
4
5
6
7
8
9

// setup log level
const log = require('loglevel');

if (process.env.NODE_LOG_LEVEL) {
  log.setDefaultLevel(process.env.NODE_LOG_LEVEL);
} else {
  log.setDefaultLevel('info');
}

How to use loglevel.disableAll:

103
104
105
106
107
108
109
110
111
112
  case WARNING:
    // loglevel's warning name is different from webpack's
    log.setLevel('warn');
    break;
  case NONE:
    log.disableAll();
    break;
  default:
    log.error('[WDS] Unknown clientLogLevel \'' + level + '\'');
}

How to use loglevel.methodFactory:

209
210
211
212
213
214
215
216
217
218
There's clearly enough enthusiasm for this even at that cost though that loglevel now includes a plugin API. To use it, redefine log.methodFactory(methodName, logLevel, loggerName) with a function of your own. This will be called for each enabled method each time the level is set (including initially), and should return a function to be used for the given log method, at the given level, for a logger with the given name. If you'd like to retain all the reliability and features of loglevel, it's recommended that this wraps the initially provided value of `log.methodFactory`

For example, a plugin to prefix all log messages with "Newsflash: " would look like:

```javascript
var originalFactory = log.methodFactory;
log.methodFactory = function (methodName, logLevel, loggerName) {
    var rawMethod = originalFactory(methodName, logLevel, loggerName);

    return function (message) {

How to use loglevel.trace:

38
39
40
41
42
43
44
45
46
47
/**
 * @param {boolean} [ignoreStderr]
 * @returns {string}
 */
build(ignoreStderr) {
  log.trace('build()', this);
  let cmd = this.cmd;
  for (const arg of this.args) {
    cmd += ' ' + arg;
  }

How to use loglevel.getLogger:

6
7
8
9
10
11
12
13
14
15
const console = global.console;

class Logger {
  constructor(loggerName) {
    this.loggerName = loggerName;
    this.logger = logging.getLogger(loggerName);
    Object.keys(logging.levels).forEach((methodName) => {
      const name = methodName.toLowerCase();
      const logFunction = this.logger[name];
      if (logFunction) {

How to use loglevel.setLevel:

11
12
13
14
15
16
17
18
19
## Features

### Simple

* Log things at a given level (trace/debug/info/warn/error) to the console object (as seen in all modern browsers & node.js)
* Filter logging by level (all the above or 'silent'), so you can disable all but error logging in production, and then run log.setLevel("trace") in your console to turn it all back on for a furious debugging session
* Single file, no dependencies, weighs in at 1.1KB minified and gzipped

### Effective

How to use loglevel.default:

136
137
138
139
140
141
142
143
144
145
if (!this._sentryLogger) return null;
//if not yet completed, set up the Sentry logger, restricting its output to
//  Sentry alone (nothing redundant to console) and with a default of
//  emitting only errors (or worse)
if ('boolean' == typeof this._sentryLogger) {
  this._sentryLogger = loglevel_1.default.getLogger('Beefy Sentry');
  this._sentryLogger.methodFactory = () => () => void 0;
  new loglevel_sentry_1.default(Sentry).install(this._sentryLogger);
  this._sentryLogger.setLevel(loglevel_1.default.levels.ERROR);
}

How to use loglevel.warn:

56
57
58
59
60
61
62
63
64
65

### AMD (e.g. RequireJS)

```javascript
define(['loglevel'], function(log) {
   log.warn("dangerously convenient");
});
```

### Directly in your web page:

How to use loglevel.error:

550
551
552
553
554
555
556
557
558
559
	log.debug('block {');
	block = parseBlock();
	log.debug('}');
	ensureHandle(block);
             if(!block.name)
                 log.error('block with handle "' + block.handle + '" is missing a name.');
	else
                 blocks[block.name] = block;
} else {
	logUnhandledGroup(curr);

How to use loglevel.info:

59
60
61
62
63
64
65
66
67
68

/**
 * @returns {Promise.<void>}
 */
async scan() {
  log.info('Scanning');
  await Process.spawn(scanimageCommand.scan(this.request));
}

/**

How to use loglevel.debug:

16
17
18
19
20
21
22
23
24
25
* Filter logging by level (all the above or 'silent'), so you can disable all but error logging in production, and then run log.setLevel("trace") in your console to turn it all back on for a furious debugging session
* Single file, no dependencies, weighs in at 1.1KB minified and gzipped

### Effective

* Log methods gracefully fall back to simpler console logging methods if more specific ones aren't available: so calls to log.debug() go to console.debug() if possible, or console.log() if not
* Logging calls still succeed even if there's no console object at all, so your site doesn't break when people visit with old browsers that don't support the console object (here's looking at you IE) and similar
* This then comes together giving a consistent reliable API that works in every JavaScript environment with a console available, and never breaks anything anywhere else

### Convenient