loglevel

repository·main·Indexed 25 days ago

https://github.com/pimterry/loglevel

A minimal, lightweight, and reliable logging library for JavaScript that works in browsers, Node.js, and other environments. It extends the standard console API with level-based filtering (trace, debug, info, warn, error) and supports named loggers, persistence via LocalStorage or cookies, and a methodFactory API for plugins.

Tokens
2.3K
Snippets
11
Records
23
Agent score
33%

What's inside loglevel

  1. Configure TypeScript for global loglevel usage

    main

    If you are using TypeScript and want to use loglevel as a global variable, you must declare it manually.

    1. Create a loglevel.d.ts file.
    2. Ensure the file is included in your build (via tsconfig.json include, command line, or a triple-slash reference).
    3. Add the following content to loglevel.d.ts:
    import * as log from 'loglevel';
    export as namespace log;
    export = log;
  2. Set up loglevel as an ES6 module

    main

    Since loglevel is a UMD module, different bundlers/transpilers may require different import syntaxes.

    For most tools, use the default import:

    import log from 'loglevel';
    log.warn("module-tastic");

    If that doesn't work for your environment, try a wildcard import:

    import * as log from 'loglevel';
    log.warn("module-tastic");
  3. Set and persist log levels with setLevel()

    main

    Use log.setLevel(level, [persist]) to disable all logging below the specified level.

    Arguments:

    • level: Can be a log level name (e.g., 'error', 'warn', 'info', 'debug', 'trace', or 'silent'), a numeric index from 0 (trace) to 5 (silent), or a value from the internal log.levels list (e.g., log.levels.SILENT) for type safety.
    • persist (optional): A boolean. If true (default), the level is saved to LocalStorage or cookies. If false, persistence is skipped.
  4. Write a plugin using the methodFactory API

    main

    You can extend loglevel by redefining log.methodFactory. This function is called for each enabled method whenever the log level is set. It should return a function that performs the actual logging.

    To maintain the original functionality and stacktrace reliability, it is recommended to wrap the existing log.methodFactory rather than replacing it entirely.

    Important: After redefining log.methodFactory, you must call log.rebuild() to apply the changes.

    var originalFactory = log.methodFactory;
    log.methodFactory = function (methodName, logLevel, loggerName) {
        var rawMethod = originalFactory(methodName, logLevel, loggerName);
    
        return function (message) {
            rawMethod("Newsflash: " + message);
        };
    };
    log.rebuild(); // Be sure to call the rebuild method in order to apply plugin.
  5. Use noConflict() to avoid global name collisions

    main

    If another library is already using the log global variable, call log.noConflict() immediately after loading loglevel. This restores the original value of the log global and returns the loglevel instance, which you can assign to a different variable name.

    <script src="loglevel.min.js"></script>
    <script>
    var logging = log.noConflict();
    
    logging.warn("still pretty easy");
    </script>
  6. Set a default log level with setDefaultLevel()

    main
    Use log.setDefaultLevel(level) to set the current log level only if no level has been previously persisted or loaded. This is ideal for initializing modules in production (e.g., setting to 'error') while allowing developers to override the level via setLevel() during debugging without the setting resetting on page refresh.
  7. Reset log levels with resetLevel()

    main
    Use log.resetLevel() to clear any persisted log level and reset the current level to the logger's default level. If no explicit default was set, it resets to the root logger's level or WARN.
  8. Get the current log level

    main

    Use log.getLevel() to return the current logging level as a number from 0 (trace) to 5 (silent). This is useful for optimizing performance by checking if expensive data generation is required before logging.

    if (log.getLevel() <= log.levels.DEBUG) {
      var logData = runExpensiveDataGeneration();
      log.debug(logData);
    }