Logger

Enable shared and custom logging in your applications.

// register extension
const Logger = Grown.use(require('@grown/logger'));

Logger.getLogger().info('This works?');
Logger.setLevel('debug');

// access through utils
Grown.use((_, util) => {
  util.getLogger().printf('\r{%ok 42%}\n');
});

// always show, not restricted by level
Logger.message('OK');
Logger.error('ERROR');

// register middleware
server.plug(Grown.Logger);

// use with server
server.mount(conn => {
  conn.res.end('DONE');

  // shown, because `debug > info`
  conn.logger.info('INFO');
  conn.logger.debug('DEBUG');

  // not shown, because `verbose > debug`
  conn.logger.verbose('VERBOSE');
});

Click ▷ RUN above and then try requesting through this link below.

Methods mixin

  • logger — Log-pose instance, see docs
    • isEnabled() — Returns if logging is globally enabled.
    • isVerbose(), isDebug() and isInfo() — Returns true if log-level is enabled, respectively.
    • printf(...) and write(...) — Low-level methods from log-pose internals
    • verbose(...), debug(...) and info(...) — High-level methods from log-pose

Public methods static

  • $before_render(ctx, template) — Append or replace {elapsed} on the response body, only if Render is available
  • $before_send(e, ctx) — Hook for calculating the {elapsed} time before sending anything, see above
  • $install(ctx) — Used by server.plug calls.
  • $mixins() — Extra Grown.Conn.Builder definitions.
  • message(...) and error(...) — Standard logging helpers.
  • pause() and resume() — To turn off/on logging respectively.
  • setLevel(type) — Set active level of logging.
  • setLogger([stdout]) — Define output target (default to process.stdout)
  • getLogger(depth[, stdout]) — Return shared logger instance, optional output as above
  • newLogger(prefix, level, depth[, stdout]) — Returns a custom log-pose interface

Private* methods static

  • _elapsedTime() — Used by $before_render.
  • _errorLog(value) — Override output for errors.
  • _msgLog(value) — Override output for regular logs.