# @stgdp/fancy-logger

> Bring some color to your console outputs!

Latest version **1.0.0** (published 2022-06-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install @stgdp/fancy-logger
pnpm add @stgdp/fancy-logger
yarn add @stgdp/fancy-logger
bun add @stgdp/fancy-logger
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2022-06-11 |
| First published | 2020-09-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=12 |
| Dependencies | 3 |
| Unpacked size | 34 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Andrew Palfrey |
| Maintainers | apalfrey |
| Keywords | console, log, logger, logging, modifiers, decoration |

## Links

- npm: https://www.npmjs.com/package/@stgdp/fancy-logger
- Repository: https://github.com/stgdp/fancy-logger
- Issues: https://github.com/stgdp/fancy-logger/issues
- npm.io page: https://npm.io/package/@stgdp/fancy-logger

## Dependencies (3)

- [fecha](https://npm.io/package/fecha.md) ^4.2.3
- [strip-ansi](https://npm.io/package/strip-ansi.md) ^6.0.1
- [@stgdp/ansi-codes](https://npm.io/package/@stgdp/ansi-codes.md) 1.0.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.0 (latest) — 2022-06-11
- 1.0.0-beta.4 (beta) — 2020-11-25
- 1.0.0-beta.3 — 2020-11-11
- 1.0.0-beta.2 — 2020-09-14
- 1.0.0-beta.1 — 2020-09-10
- 1.0.0-beta.0 — 2020-09-10

## README

# @stgdp/fancy-logger

Bring some color to your console outputs!

[![GitHub release](https://img.shields.io/github/v/release/stgdp/fancy-logger?include_prereleases&style=flat-square)](https://github.com/stgdp/fancy-logger/releases/latest)
[![npm Version](https://img.shields.io/npm/v/@stgdp/fancy-logger?style=flat-square)](https://www.npmjs.com/package/@stgdp/fancy-logger)
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/stgdp/fancy-logger/Linting%20&%20testing?style=flat-square)](https://github.com/stgdp/fancy-logger/actions/workflows/lint-test.yml)
[![License](https://img.shields.io/github/license/stgdp/fancy-logger?style=flat-square)](LICENSE)

`fancy-logger` is a Node.js module that supplies you with a fully-featured console logger to format your terminal. Change the text color, background color, make the text bold, italic, underline and more.

## Installation

### With npm

```
npm install @stgdp/fancy-logger
```

### With yarn

```
yarn add @stgdp/fancy-logger
```

## Usage

```javascript
const logger = require( '@stgdp/fancy-logger' )

// Produces a bold console log with a white background and red text
logger()
    .red
    .bold
    .bg
    .white
    .write( 'I\'m formatted!' )
    .end
// OR
logger( {
    modifiers: {
        fg: 'red',
        bg: 'white',
        decoration: {
            bold: true,
        },
    },
} )
    .write( 'I\'m formatted!' )
    .end
```

## Reference

### logger( ?options: Object )

Starts the logger with a timestamp by default. Options can be supplied to the logger in an object.

| Options        | Default    | Operation                                                                                                                                                         |
| -------------- | ---------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `timestamp`    | `true`     | Enables the timestamp                                                                                                                                             |
| `format`       | `HH:mm:ss` | Sets the format for the timestamp. Check out the [fecha documentation](https://github.com/taylorhakes/fecha#formatting-tokens) for the formatting characters.     |
| `buffer`       | `false`    | Buffers the output to be returned or outputted at a later time                                                                                                    |
| `file`         | `false`    | Sets a file to store the current log buffer to when `to_file` is called                                                                                           |
| `file_options` | `{}`       | Sets the file options for the `to_file` method. Gets passed to `fs.writeFileSync`. See more: https://nodejs.org/api/fs.html#fs_fs_writefilesync_file_data_options |
| `modifiers`    | `{}`       | Sets the default modifiers for the logger to run with. See: [Default Modifiers](#default-modifiers)                                                               |

#### Usage

```javascript
// Without options
logger()

// With options
logger( {
    timestamp: true,
    format: 'HH:mm:ss',
    buffer: false,
    file: './logs/node-app.log',
    file_options: {
        encoding: 'utf8',
    },
} )
```

### write( content: String, modifiers: Object )

Outputs provided content to the console through the logger. When the `buffer` option is set to `true`, this will be stored within the logger until either `output` or `return` are called instead.

`modifiers` uses the [Default Modifiers](#default-modifiers) with an additional parameter of `reset` for [reset modifiers](#reset)

#### Usage

```javascript
// Outputs "hello world" to the console with a timestamp and new line.
logger()
    .write( 'hello world' )
    .end

// Outputs "hello world" to the console with a timestamp, new line and blue "world".
logger()
    .write( 'hello ' )
    .write( 'world', {
        fg: 'blue',
    } )
    .end
```

### to_file( file: String )

Writes the current log to file. Recommended to be used at the end of a log to prevent the log from being written to file twice. The `file` parameter can be left blank to use the default set in the `logger` constructor, or be set to a file location.

#### Usage

```javascript
logger( {
    file: './logs/node-app.log',
} )
    .write( 'hello world' )
    .end
    .to_file()
// OR
logger( {
    file: path.resolve( './logs/node-app.log' ),
} )
    .write( 'hello world' )
    .end
    .to_file()
// OR
logger()
    .write( 'hello world' )
    .end
    .to_file( './logs/node-app.log' )
// OR
logger()
    .write( 'hello world' )
    .end
    .to_file( path.resolve( './logs/node-app.log' ) )
```

### end

Resets all styles and outputs a new line to the console through the logger.

#### Usage

```javascript
// Outputs "hello world" to the console with a timestamp and new line.
logger()
    .write( 'hello world' )
    .end
```

### output

Outputs the buffer to the console through the logger.

#### Usage

```javascript
// Outputs "hello world" to the console with a timestamp and new line.
logger( {
    buffer: true,
} )
    .write( 'hello world' )
    .end
    .output
```

### return

Returns the buffer created by the logger, along with the ansi codes for the modifiers used.

#### Usage

```javascript
// Returns "hello world"
var msg = logger( {
    buffer: true,
} )
    .write( 'hello world' )
    .end
    .return
```

### Modifier types

Modifier types adjust a parameter within the logger system, allowing the console to have it's foreground, background or decoration changed. The following modifier types are available;

| Modifier type | Description                                                                                                           |
| ------------- | --------------------------------------------------------------------------------------------------------------------- |
| `fg`          | Allows for setting a foreground color modifier (e.g. `red`)                                                           |
| `bg`          | Allows for setting a background color modifier (e.g. `red`)                                                           |
| `decoration`  | Allows for setting a decoration modifier (e.g. `bold`)                                                                |
| `reset`       | Allows for issuing a reset modifier (e.g. `all`)                                                                      |
| `bright`      | Alters the foreground and background modifiers to use the bright variant. This modifier type works on a toggle system |

### fg and bg

Foreground and background modifiers allow the logger to change their foreground and background colors respectively. While foreground modifiers only need to be preceeded by the `fg` modifier type if another modifier type was previously used, background modifiers always need to be preceeded by the `bg` modifier type. Bright variants can be accessed by using the `bright` modifier type.

#### Available modifiers

-   `black`
-   `red`
-   `green`
-   `yellow`
-   `blue`
-   `magenta`
-   `cyan`
-   `white`

#### Usage

```javascript
// Sets the logger foreground color to red
logger()
    .red
    .write( 'hello world' )
    .end
logger()
    .fg
    .red
    .write( 'hello world' )
    .end

// Sets the logger background color to red
logger()
    .bg
    .red
    .write( 'hello world' )
    .end

// Sets the logger background color to bright red
logger()
    .bg
    .bright
    .red
    .write( 'hello world' )
    .end
```

### decoration

Decoration modifiers allow the logger to change their decorations. These modifiers only have to be preceeded by the `decoration` modifier type is another modifier type was previously used.

#### Available modifiers

-   `bold`
-   `dim`
-   `italic`
-   `underline`
-   `inverse`
-   `hidden`
-   `strike`
-   `frame`
-   `encircle`
-   `overline`

#### Usage

```javascript
// Sets the logger decoration to bold
logger()
    .bold
    .write( 'hello world' )
    .end
logger()
    .decoration
    .bold
    .write( 'hello world' )
    .end
```

### reset

Reset modifiers allow the logger to reset previously applied modifiers back to their default values. All modifiers except for `all`, `reset_fg` and `reset_bg` have to be preceeded by the `reset` modifier type.

#### Available modifiers

-   `all`
-   `bold`
-   `dim`
-   `italic`
-   `underline`
-   `inverse`
-   `hidden`
-   `strike`
-   `reset_fg`
-   `reset_bg`
-   `frame`
-   `encircle`
-   `overline`

#### Usage

```javascript
// Resets all modifiers
logger()
    .red
    .bold
    .write( 'hello' )
    .all
    .write( ' world' ).end
logger()
    .red
    .bold
    .write( 'hello')
    .reset
    .all
    .write(' world')
    .end

// Resets the bold modifier, leaving the foreground modifier alone
logger()
    .red
    .bold
    .write( 'hello')
    .reset
    .bold
    .write(' world')
    .end
```

### Default Modifiers

Default modifiers can be sent with the logger options to style the log without needing to chain modifiers. **NOTE:** You don't have to send the full object, only send what you need and leave the rest.

#### Usage

```javascript
// Output will have a bright red foreground, blue background and be bold
logger( {
    modifiers: {
        fg: 'red',
        bg: 'blue',
        bright: {
            fg: true,
            bg: false,
        },
        decoration: {
            bold: true,
            dim: false,
            italic: false,
            underline: false,
            inverse: false,
            hidden: false,
            strike: false,
            frame: false,
            encircle: false,
            overline: false,
        },
    },
} )
    .write( 'hello world' )
    .end
// OR
logger( {
    modifiers: {
        fg: 'red',
        bg: 'blue',
        bright: ['fg'],
        decoration: ['bold'],
    },
} )
    .write( 'hello world' )
    .end
```

## License

```
MIT License

Copyright (c) 2020 Andrew Palfrey <apalfrey@apalfrey.me>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```

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