# Usage Examples

# Default logging

What's Shown

Log a string to the console with a timestamp, code location from the stack trace, and colors

import { Warn } from "multi-level-logger";

function helloLogs() {
    Log(`Hello multi-level-logger`);
}

helloLogs();
1
2
3
4
5
6
7
> 1/1/2018, 12:00:00 PM - helloLogs() [line 4: ./log-example.js] - Hello multi-level-logger

# Turn off timestamp and code location

What's Shown

Log an object to the console at the Info level without a timestamp or code location

import { LogLevels, InitializeLogging, OutputFormatting, Info } from "multi-level-logger";

InitializeLogging(LogLevels.info);
OutputFormatting({ includeTimestamp: false, includeCodeLocation: false });

Info({ prop1: true, prop2: "yes" });
1
2
3
4
5
6
{
    "prop1": true,
    "prop2": "yes"
}

# Override configuration on a single log call

What's Shown

Configure defaults to not show timestamp or code location, then log an object at the DEBUG level with the timestamp

import { InitializeLogging, OutputFormatting, Debug, Info } from "multi-level-logger";

InitializeLogging(15);
OutputFormatting({ includeTimestamp: false, includeCodeLocation: false });

Info("Using defaults");

Debug({ prop1: true, prop2: "yes" }, { configuration: { includeTimestamp: true } });
1
2
3
4
5
6
7
8
> Using defaults
> 1/1/2018, 12:00:00 PM - {
                              "prop1": true,
                              "prop2": "yes"
                          }

# Catch and log any application errors as fatal errors

What's Shown

Error message and stack trace using colorized logging

const { Fatal } = require(`multi-level-logger`);

async function runProgram() {
    throw new Error(`here`);
}

runProgram()
    .catch(err => {
        Fatal(err);
    });
1
2
3
4
5
6
7
8
9
10
1/1/2018, 12:00:00 PM - runProgram.catch.err() [line 9: ./log-example.js] - Error: here
    at runProgram (./log-example.js:4:11)
    at Object.<anonymous> (./log-example.js:7:1)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

# Turn off log colors

What's Shown

Logging an object at the Warn level with colors turned off

const { OutputFormatting, Warn } = require(`multi-level-logger`);

OutputFormatting({ useColors: false });

Warn({ prop1: true, prop2: "yes" });
1
2
3
4
5
1/1/2018, 12:00:00 PM - [line 5: ./log-example.js]
     {
         "prop1": true,
         "prop2": "yes"
     }

1
2
3
4
5
6