# simplr-logger

> Simple JavaScript logger written in TypeScript that can be used in browser or node application.

Latest version **1.0.1** (published 2017-11-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install simplr-logger
pnpm add simplr-logger
yarn add simplr-logger
bun add simplr-logger
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2017-11-28 |
| First published | 2017-11-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | SimplrJS |
| Maintainers | simplrjs-admin, quatrodev |
| Keywords | typescript, javascript, logger, simplr, console, node, file-system |

## Links

- npm: https://www.npmjs.com/package/simplr-logger
- Repository: https://github.com/SimplrJS/simplr-logger
- Homepage: https://github.com/SimplrJS/simplr-logger#readme
- Issues: https://github.com/SimplrJS/simplr-logger/issues
- npm.io page: https://npm.io/package/simplr-logger

## Dependencies (1)

- [tslib](https://npm.io/package/tslib.md) ^1.8.0

## Alternatives

- [cli-color](https://npm.io/package/cli-color.md) — 3.4M weekly downloads
- [log](https://npm.io/package/log.md) — 1.3M weekly downloads
- [logstash-client](https://npm.io/package/logstash-client.md) — 4.5K weekly downloads
- [@nocobase/plugin-logger](https://npm.io/package/@nocobase/plugin-logger.md) — 2.0K weekly downloads
- [child-process-debug](https://npm.io/package/child-process-debug.md) — 695 weekly downloads

## Recent versions

- 1.0.1 (latest) — 2017-11-28
- 1.0.0-beta.6 (beta) — 2017-11-24
- 0.4.0-alpha.6 (alpha) — 2017-11-20
- 1.0.0 — 2017-11-27
- 1.0.0-beta.5 — 2017-11-24
- 1.0.0-beta.4 — 2017-11-24
- 1.0.0-beta.3 — 2017-11-23
- 1.0.0-beta.2 — 2017-11-23
- 1.0.0-beta.1 — 2017-11-23
- 0.4.0 — 2017-11-20
- 0.3.0 — 2017-11-16
- 0.2.1 — 2017-11-14
- 0.2.0 — 2017-11-14
- 0.1.1 — 2017-11-10
- 0.1.0 — 2017-11-10

## README

# Simplr Logger

[![NPM version](http://img.shields.io/npm/v/simplr-logger.svg)](https://www.npmjs.com/package/simplr-logger)
[![Build Status](https://travis-ci.org/SimplrJS/simplr-logger.svg?branch=master)](https://travis-ci.org/SimplrJS/simplr-logger)
[![Coverage Status](https://coveralls.io/repos/github/SimplrJS/simplr-logger/badge.svg)](https://coveralls.io/github/SimplrJS/simplr-logger)
[![dependencies Status](https://david-dm.org/simplrjs/simplr-logger/status.svg)](https://david-dm.org/simplrjs/simplr-logger)
[![devDependencies Status](https://david-dm.org/simplrjs/simplr-logger/dev-status.svg)](https://david-dm.org/simplrjs/simplr-logger?type=dev)

Simple JavaScript logger written in [TypeScript](http://typescriptlang.org) that can be used in browser or node application.

The package is most useful when used with [TypeScript](http://typescriptlang.org).

## Get started

```cmd
npm install simplr-logger
```

### Building logger

#### With default configuration

```ts
import { LoggerBuilder } from "simplr-logger";

const logger = new LoggerBuilder();
```

#### With configuration builder

```ts
import { LoggerBuilder, LoggerConfigurationBuilder, LogLevel } from "simplr-logger";
import { FileMessageHandler, ConsoleMessageHandler } from "simplr-logger/handlers";

const config = new LoggerConfigurationBuilder()
    .SetDefaultLogLevel(LogLevel.Trace)
    .AddWriteMessageHandlers([
        { Handler: new ConsoleMessageHandler() },
        { Handler: new FileMessageHandler("./logs.txt") }]
    )
    .Build();

const logger = new LoggerBuilder(config);
```

#### With simple object

```ts
import { LoggerBuilder, LogLevel, ConsoleMessageHandler } from "simplr-logger";

const logger = new LoggerBuilder({
    DefaultLogLevel: {
        LogLevel: LogLevel.Trace,
        LogLevelIsBitMask: false
    },
    WriteMessageHandlers: [{
        Handler: new ConsoleMessageHandler(),
        LogLevel: LogLevel.Critical | LogLevel.Debug,
        LogLevelIsBitMask: true
    }]
});
```

### Creating logger handler

```ts
import { MessageHandlerBase, LogLevel, LoggerBuilder, LoggerConfigurationBuilder } from "simplr-logger";

class MyMessageHandler extends MessageHandlerBase {
    public HandleMessage(level: LogLevel, timestamp: number, messages: any[]): void {
        console.log(...messages);
    }
}

const config = new LoggerConfigurationBuilder()
    .AddWriteMessageHandler({ Handler: new MyMessageHandler(), LogLevel: LogLevel.Trace })
    .Build();

const logger = new LoggerBuilder(config);

```

### Using logger

#### Logging with methods

```ts
logger.Critical("Critical", "message");
logger.Debug("Debug", "message");
logger.Error("Error", "message");
logger.Info("Info", "message");
logger.Warn("Warn", "message");
logger.Trace("message", "with trace");
```

#### Logging with log level

```ts
logger.Log(LogLevel.Information, "Info message");
logger.Log(LogLevel.Critical, new Error("Critical error"));
```

#### Updating configuration

```ts
// Using old configuration
logger.UpdateConfiguration(builder => builder.SetPrefix("[new prefix]").Build());

// Or with default configuration
logger.UpdateConfiguration(builder => builder.SetPrefix("[new prefix]").Build(), false);
```

## API

### [LogLevel](./src/abstractions/log-level.ts)

|     Name    | Value | Description                                                                                                                                                                                          |
|-------------|:-----:|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|     None    |   0   | Not used for writing log messages. Specifies that a logging category should not write any messages.                                                                                                  |
|   Critical  |   1   | Logs that describe an unrecoverable application or system crash, or a catastrophic failure that requires immediate attention. |                                                                      |
|    Error    |   2   | Logs that highlight when the current flow of execution is stopped due to a failure. These should indicate a failure in the current activity, not an application-wide failure.                        |
|   Warning   |   4   | Logs that highlight an abnormal or unexpected event in the application flow, but do not otherwise cause the application execution to stop.                                                           |
| Information |   8   | Logs that track the general flow of the application. These logs should have long-term value.                                                                                                         |
|    Debug    |   16  | Logs that are used for interactive investigation during development. These logs should primarily contain information useful for debugging and have no long-term value.                               |
|    Trace    |   32  | Logs that contain the most detailed messages. These messages may contain sensitive application data. These messages are disabled by default and should never be enabled in a production environment. |

### Configuration

| Name                  | Default value                                                              | Description                                                        |
|-----------------------|----------------------------------------------------------------------------|--------------------------------------------------------------------|
|  WriteMessageHandlers | `[{ Handler: new ConsoleMessageHandler() }]`                               | Message handlers list. ⁽¹⁾                                         |
|  DefaultLogLevel      | `{ LogLevel: LogLevel.Warning, LogLevelIsBitMask: false }`                 | Log level or log levels in bit mask value.                         |
|  Prefix               | `undefined`                                                                | Custom message, which will be injected into the start of messages. |

`(1)` - The default value is only available if configuration property is not set.

## License

Released under the [MIT license](LICENSE).

---
_Source: https://npm.io/package/simplr-logger · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
