How to use the getLogger function from loglevel

Find comprehensive JavaScript loglevel.getLogger code examples handpicked from public code repositorys.

loglevel.getLogger is a method provided by the loglevel library that creates a new logger instance with a specified name.

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) {
fork icon498
star icon2
watch icon3

+ 3 other calls in file

171
172
173
174
175
176
177
178
179
180

// In your main application module:
var log = require("loglevel");
var moduleOne = require("module-one");
var moduleTwo = require("module-two");
log.getLogger("module-two").setLevel("TRACE");

moduleOne.doSomethingAmazing();
moduleTwo.doSomethingSpecial();
// logs "Special message from module two."
fork icon170
star icon0
watch icon4

How does loglevel.getLogger work?

loglevel.getLogger is a function provided by the loglevel library that returns a logger object with specified name and log level. The returned logger object can be used to log messages with different severities, such as debug, info, warn, error, etc.

13
14
15
16
17
18
19
20
21
22
23
24
prefix.reg(rootLog);
rootLog.enableAll();
rootLog.setLevel(config.log.level);
prefix.apply(rootLog, config.log.prefix);


const log = rootLog.getLogger('Http');
const api = require('./api');


/**
 * @param {import('express').Response} res
fork icon118
star icon474
watch icon0

13
14
15
16
17
18
19
20
21
constructor(moduleName, logLevel) {
  if (!moduleName) {
    throw Errors.INVALID_ARGUMENT.clone('Error instantiating Logger. <string>moduleName is a required parameter.');
  }

  this._log = log.getLogger(moduleName);
  logLevels.forEach(function(level) {
    this[level] = (...args) => this._log[level](this._getTimestamp(), ...args);
  }.bind(this));
fork icon28
star icon20
watch icon0

+ 3 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
import log from "loglevel";

const logger = log.getLogger("myLogger");

logger.setLevel("trace");

logger.trace("This is a trace message");
logger.debug("This is a debug message");
logger.info("This is an info message");
logger.warn("This is a warning message");
logger.error("This is an error message");

In this example, we first import the loglevel library and create a new logger instance named myLogger using the getLogger method. We then set the log level to trace using the setLevel method, which allows all messages to be logged. Finally, we use the logger to output log messages at different levels using the trace, debug, info, warn, and error methods. The output of these messages depends on the log level set for the logger.

15
16
17
18
19
20
21
prefix.apply(root_log, {
  format(level:any, name:any) {
    return `${chalk.green(`${name}: ${colors[level.toUpperCase()](level)}`)}`;
  },
});
const log = root_log.getLogger('wmtutor');
export { log }
fork icon2
star icon1
watch icon4

9
10
11
12
13
14
15
16
17
18
19
20
const fetch = require('node-fetch');


const nautilusApiKey = process.env.NAUTILUS_API_KEY;
if(!nautilusApiKey) throw new Error('Missing NAUTILUS_API_KEY environment variable');


const logger = log.getLogger('conseiljs');
logger.setLevel('error', false);
registerLogger(logger);
registerFetch(fetch);
const conseilServer = 'https://conseil-prod.cryptonomic-infra.tech';
fork icon0
star icon2
watch icon0

+ 2 other calls in file

45
46
47
48
49
50
51
52
53
54
55
56
  return (...args) => rawMethod(
    `[${chalk.blue((new Date()).toISOString())} ${context}]`, ...args);
};


/** get a new logger; call with a name, e.g., `module.id` */
const getLogger = loglevel.getLogger;


/** Deep-clone the given object. All functionality is lost, just data is kept. */
const clone = (obj) => JSON.parse(JSON.stringify(obj));

fork icon0
star icon0
watch icon1

52
53
54
55
56
57
58
59
60
61
    ? conversationOrApplication
    : null;
const application = conversationOrApplication instanceof application_1.default
    ? conversationOrApplication
    : null;
this.log = loglevel_1.getLogger(this.constructor.name);
if (conversation) {
    this.rtcHelper = new rtc_helper_1.default();
    this.application = conversation.application;
    this.application.activeStreams = this.application.activeStreams || [];
fork icon0
star icon0
watch icon1

+ 3 other calls in file