# loggol

> Configurable logging module written in TypeScript

Latest version **0.0.4** (published 2018-06-20) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install loggol
pnpm add loggol
yarn add loggol
bun add loggol
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.4 |
| Published | 2018-06-20 |
| First published | 2018-06-18 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 29.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Damon |
| Maintainers | damon_ |
| Keywords | logger, colour, file, TypeScript, chain, format, custom |

## Links

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

## Alternatives

- [unionfs](https://npm.io/package/unionfs.md) — 2.2M weekly downloads
- [path-starts-with](https://npm.io/package/path-starts-with.md) — 35.9K weekly downloads
- [redzip](https://npm.io/package/redzip.md) — 1.2K weekly downloads
- [vscode-anymatch](https://npm.io/package/vscode-anymatch.md) — 848 weekly downloads
- [@ledgerhq/coin-filecoin](https://npm.io/package/@ledgerhq/coin-filecoin.md) — 793 weekly downloads

## Recent versions

- 0.0.4 (latest) — 2018-06-20
- 0.0.3 — 2018-06-19
- 0.0.2 — 2018-06-18
- 0.0.1 — 2018-06-18

## README

# Introduction

Simple logging module written in TypeScript.

# Levels

There are 5 log levels:
```js
const Logger = require("loggol")

Logger.Level.DEBUG
Logger.Level.INFO
Logger.Level.WARN
Logger.Level.ERROR
Logger.Level.OFF
```

# Logging to the console

The following code creates a logger object that writes to stdout 
and stderr in the format `[dd/mm/yyyy hh:mm:ss][LEVEL][TAG]: message]`:
```js
const Logger = require("loggol")

const logger = new Logger.Console({
	tag: "Logger1",
	level: Logger.Level.DEBUG,
	format: Logger.Format.standard
});

logger.debug("debug");
logger.info("info");
logger.warn("warn");
logger.error("error");
```

If you would like colours and your terminal supports it, replace
`Logger.Format.standard` with `Logger.Format.standardColoured`

# Logging to a file

The following code creates a logger object that writes to a file
stream of your choice, in the same format as above
```js
const fs = require("fs");
const Logger = require("loggol")

const logger = new Logger.File({
	tag: "Logger2",
	level: Logger.Level.DEBUG,
	format: Logger.Format.standard,
	file: fs.createWriteStream("output.log", {
		flags: "a",
		encoding: "utf-8"
	})
});
```

# Chaining loggers

Often you could find yourself wanting to log the
same data to two or more different output, you can do this
by using the chain method of the `Logger` prototype
```js
const Logger = require("loggol");

const logger1 = new Logger.Console(...);
const logger2 = new Logger.File(...);
const logger3 = new Logger.File(...);

logger1
	.chain(logger2)
	.chain(logger3)

logger1.info("hello!");
// logs to logger2 and logger3 as well

logger2.info("wow");
// logs to logger3, but not logger1 
```

# Creating your own format

Format functions receive a `LogData` object:
```ts
type LogData = {
	date: Date;
	tag: string;
	level: number;
	items: any[]
};
```
Just to clarify, since the others are self explanatory, items is
just an array of whatever the calling code wanted to log.

Your function should look something like this:
```js
function myFormat({ date, tag, level, items }) {
	// create output here
	// return string
}
```
Then you can pass this function around to all your loggers that
need to use it.

# Creating your own Logger

If the included implemenations don't quite fit your needs you can
always implement your own and keep the same public interface.

```js
const Logger = require("loggol");

class OtherLogger extends Logger.Abstract {
	write(message) {
		// whatever needs to happen to log 'message'
	}
}
```

# Logger factories

Sometimes you may not want to use a single logger with
the same tag and level. A function that you can
import everywhere a logger is needed is great for creating
a bunch of similar logger objects.

For example, a lot of files I have only need `Logger.Console` and
they should all have the same level and format, but a different tag.

```js
const Logger = require("loggol");
const level = Logger.Level.INFO;
const format = Logger.Format.standardColoured;

function createLogger(tag) {
	return new Logger.Console({
		tag,
		level,
		format
	});
}

module.exports = createLogger;
```
You could also do something similar to return different types of `Logger`'s

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