# als-logger

> Flexible and powerful logging utility for Node.js applications.

Latest version **3.0.0** (published 2024-09-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install als-logger
pnpm add als-logger
yarn add als-logger
bun add als-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 | 3.0.0 |
| Published | 2024-09-28 |
| First published | 2023-06-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 29.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Alex Sorkin |
| Maintainers | alexsorkin |
| Keywords | logger |

## Links

- npm: https://www.npmjs.com/package/als-logger
- npm.io page: https://npm.io/package/als-logger

## Dependencies (1)

- [als-object-serializer](https://npm.io/package/als-object-serializer.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

- 3.0.0 (latest) — 2024-09-28
- 2.0.1 — 2024-05-28
- 2.0.0 — 2024-05-08
- 1.0.0 — 2023-12-19
- 0.6.0 — 2023-09-03
- 0.5.0 — 2023-09-01
- 0.2.1 — 2023-06-08
- 0.2.0 — 2023-06-05
- 0.1.1 — 2023-06-04
- 0.1.0 — 2023-06-04

## README

# ALS Logger (Version 3.0)

`als-logger` is a flexible and powerful logging utility for Node.js applications. It supports customizable log formats, file management, error handling, and much more.


## Features

- **Customizable Logging Levels**: Easily log different message types like `log`, `warn`, and `error`.
- **File Management**: Automatically rotates log files and removes old entries based on age.
- **Error Handling**: Captures uncaught exceptions and unhandled promise rejections.

## Installation

Install the package via npm:

```bash
npm install als-logger
```

## Usage

### Basic Usage

Create an instance of the `Logger` and start logging messages.

```js
const Logger = require('als-logger');

const logger = new Logger('/path/to/logs');

// Log messages of various types
logger.log('some msg','This is a log message.');
logger.warn('some msg','This is a warning.');
logger.error('some msg','This is an error.');

```

## API

```js
const logger = new Logger(dirPath:String,options={}:Object):instanceof Logger
logger.log(...msgs):Promise
logger.warn(...msgs):Promise
logger.error(...msgs):Promise
add(type:String, msgs:Array):Promise
logger.close():undefined

logger.report:instanceof LoggerReport
logger.report.limit(limit:Number):instanceof LoggerReport
logger.report.days(days:Number):instanceof LoggerReport
logger.report.type(...types:String):instanceof LoggerReport
logger.report.get(query:Object):Promise=>array
```

### Options and validation

* `dirPath` - dir path for logs
  * Default:`undefined`
  * Validation: throwing error if dirPath no type of string 
    * if folder not exists, it will be created 
  * Limits: You can't create two instances of Logger with same directory (throwing error)
* `options`
  * Default: `{ timeout = 0, types = ['log', 'warn', 'error'], dev = false, errorFn = console, maxDays = 10 }`
* `options.timeout` - debounce timeout to write logs
  * Default `0`
  * Validate: throwing error if timeout no positive number
* options.types - logger methods from types 
  * Default:`['log', 'warn', 'error']`
  * Validation: throwing error if types not array
    * Filtering array's item which not type of string
* options.dev - developement mode. Using errorFn, instead writing logs.
  * Default:`false`
  * Validation: throwing error if dev not type of Boolean
* options.errorFn
  * Default:`console`
  * Validation: throwing error if errorFn is not and object or don't have method from `options.types` 
* options.maxDays - maxDays to save in cache
  * Default:`10`
  * Validation: throwing error if maxDays no positive number 

## Report

```js
logger.report
.limit(20) // limit for 20 logs
.days(5) // return up to 5 last days
.types('warn','error') // return only types of warn and error
.get() // return Promise which resolved to  [...,{ timestamp, type, msgs }]
```

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