Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

fusionjs/fusion-plugin-universal-logger

Repository files navigation

fusion-plugin-universal-logger

Build status

A logger plugin that can handle logging both server side and client side. On the server it uses winston directly. On the client, it batches logs and sends them via network to the server at set intervals.

Depends on fusion-plugin-universal-events.


Table of contents

Installation

yarn add fusion-plugin-universal-logger

Usage

import {LoggerToken} from 'fusion-tokens';
// ...
app.middleware({logger: LoggerToken}, ({logger}) => {
  return (ctx, next) => {
    if (__NODE__) logger.info(`Received request at ${ctx.url}`);
    else logger.info(`Pageload at ${ctx.url}`);
    return next();
  };
});

Setup

import App from 'fusion-core';
import winston from 'winston';
import UniversalEvents from 'fusion-plugin-universal-events';
import UniversalLogger, {
  UniversalLoggerConfigToken,
} from 'fusion-plugin-universal-logger';

export default () => {
  const app = new App(<div>Hello</div>);
  app.register(UniversalEventsToken, UniversalEvents);
  app.register(LoggerToken, UniversalLogger);
  if (__NODE__) {
    // optional winston configuration
    const config = {
      transports: [new winston.transports.File({filename: 'logs.log'})],
    };
    app.register(UniversalLoggerConfigToken, config);
  }
  return app;
};

API

Registration API

UniversalLogger
import UniversalLogger from 'fusion-plugin-universal-logger';

The universal logger plugin. Typically it should be registered to the LoggerToken. Provides the logger service api

LoggerToken
import {LoggerToken} from 'fusion-tokens';

fusion-plugin-universal-logger conforms to the standard logger api designated by the LoggerToken from the fusion-tokens library, and is most commonly registered with this token.

Dependencies

UniversalEventsToken
import {UniversalEventsToken} from 'fusion-plugin-universal-events';

An event emitter plugin, such as the one provided by fusion-plugin-universal-events. Required.

UniversalLoggerConfigToken
import {UniversalLoggerConfigToken} from 'fusion-plugin-universal-logger';

A Winston configuration object. Optional. Server-side only.

Service API

logger.log(level, ...args);
  • level: string - Valid levels: 'error','warn','info','verbose','debug','silly'
  • args: [string]
logger.error(...args);
  • args: [string]
logger.warn(...args);
  • args: [string]
logger.info(...args);
  • args: [string]
logger.verbose(...args);
  • args: [string]
logger.debug(...args);
  • args: [string]
logger.silly(...args);
  • args: [string]