npm package: @popovmp/logger
Description: Logger
Latest version: 2.2.0

# A simple Logger helper for Node.js

**logger** is a straightforward logger written in TypeScript. It logs to a
single predefined file. When an option `{tee: true}` is given, **logger** shows
colored messages in the console. If a log file is not defined, **logger** writes
to the console only.

Homepage: <https://github.com/popovmp/logger>

## Color outputs

![Logger output light](https://github.com/PopovMP/logger/blob/main/img/logger-outpu-light.png)

![Logger output dark](https://github.com/PopovMP/logger/blob/main/img/logger-outpu-dark.png)

## Installation

```bash
npm install @popovmp/logger
```

## Usage

```javascript
import {initLogger, logError, logSuccess, logWarning} from "@popovmp/logger";

// Initialize logger
const loggerOptions = {
  filepath: "my/logger/path/log.txt",
  tee     : true,
  suppress: ["debug"],
};

initLogger(loggerOptions);
```

## Examples

```javascript
logInfo("Hello, World!");                           // => 2020-08-21 06:21:11 [INFO] Hello, World!
logInfo("GET index", "app::router");                // => 2020-08-21 06:21:11 [INFO] [app::router] GET index
logError("Ohh!", "bank::delete-account");           // => 2020-08-21 06:21:11 [ERROR] [bank::delete-account] Ohh!
logError(new Error("Err!"), "mission :: critical"); // => 2020-08-21 06:21:11 [ERROR] [mission :: critical] Err!
logText("So Long, and Thanks for All the Fish!");   // => So Long, and Thanks for All the Fish!
````

## Last error

**logger** has two methods for getting and resetting the last logged error
message: `getLastError` and `resetLastError`.

`getLastError` returns the last logged error message by the `logError` or
`logger.error` methods.

You can reset the last error with the `resetLastError` method. When
`resetLastError` is called without parameters, it sets the last error to
`undefined`. `resetLastError` can be called with `null` to set the last error to
`null`.

```javascript
getLastError(); // undefined
logError("some eror");
getLastError(); // some error
resetLastError();
getLastError(); // undefined
```

## Options

The `filepath` option sets the log filepath.

The `init` method accepts an options `options` parameter. It has two property
`tee: boolean` and `suppress: string[]`.

When `tee` is set to `true`, the logger doubles the message on the console.

The `suppress` parameter accepts a string[]. It suppresses the logging of the
tags included.

The possible values are:

```json
{
  "suppress": ["debug", "text", "info", "error", "success"]
}
```

## Methods

**logger** exports the following methods:

```javascript
/**
 * @param {LoggerOptions} loggerOptions
 * @returns {void}
 */
initLogger(loggerOptions);
```

```javascript
/**
 * Logs a debug message to a log file
 *
 * @param {Error|object|string} message
 * @param {string} [sender]
 */
logDebug(message, sender);
```

```javascript
/**
 * Logs a message to a log file
 *
 * @param {Error|object|string} message
 * @param {string} [sender]
 */
logInfo(message, sender);
```

```javascript
/**
 * Logs an error to a log file
 *
 * @param {Error|object|string} message
 * @param {string} [sender]
 */
logError(message, sender);
```

```javascript
/**
 * Logs a success message to a log file
 *
 * @param {string} message
 * @param {string} [sender]
 */
logSuccess(message, sender);
```

```javascript
/**
 * Logs a text message
 *
 * @param {string} message
 */
logText(message);
```

```javascript
/**
 * Gets the last logged error message
 *
 * @returns {Error|object|string|undefined|null}
 */
getLastError();
```

```javascript
/**
 * Resets the last error
 *
 * @param {Error|object|string|undefined|null} [value]
 */
resetLastError(value);
```

```javascript
/**
 * Resets logger options for testing purposes
 */
resetLogger();
```

## License

`logger` is free for use and modification. No responsibilities for damages of
any kind.