Skip to content
Grant Carthew edited this page Oct 25, 2024 · 8 revisions

Method Signature

new Perj(options)

Type: Function

Parameter: undefined or an Options object.

Returns: Object

  • A logger object.

Example:

import Perj from 'perj'
const log = new Perj(options)

Description

The new key word is used to instantiate a logger objects. As you can see above it only takes one options argument in the form of a plain object. This options argument can change the function of the logger or add extra information to the logger output. See the Options and Top Level Properties documents for more detail.

Examples

Following is an example of the Options object being used to create a logger object. The options are simply being set to their default value on Node.js. This is just to show the syntax:

import Perj from 'perj'

const logOptions = {
  levels: {
    fatal: 60,
    error: 50,
    warn: 40,
    info: 30,
    debug: 20,
    trace: 10
  },
  level: 'info',
  levelKey: 'level',
  levelKeyEnabled: true,
  levelNumberKey: 'lvl',
  levelNumberKeyEnabled: true,
  dateTimeKey: 'time',
  dateTimeFunction: () => Date.now(),
  messageKey: 'msg',
  dataKey: 'data',
  separatorString: ':',
  serializers: false,
  serializeErrorFunction: (err) => { /* serialize-error.js */ },
  stringifyFunction: (obj) => { /* stringify.js */ },
  passThrough: false,
  write: process.stdout.write.bind(process.stdout)
}

const log = new Perj(logOptions)

The following example adds a name, host, pid, and file as top level properties to the log entries. These properties do not exist by default because that would require perj to import the os Node.js module. Node.js modules are not imported to ease the use of perj on either Node.js or in the Browser.

import Perj from 'perj';
import os from 'os';
import path from 'path';

// Customize the variables below as needed. They are not required.
const ver = 1;
const name = 'QuickStart';
const host = os.hostname();
const pid = process.pid;
const file = path.basename(import.meta.url, '.js');

const log = new Perj({ ver, name, host, pid, file });

/*

The following will be sent to the process standard out:

{"level":"info","lvl":30,"ver":1,"name":"QuickStart","host":"Dev","pid":56795,"file":"quick.js","time":1526212446856,"msg":"the quick brown fox jumps over the lazy dog","data":""}

*/