![MK Simple Logger](https://marck-devs.com/assets/img/SimpleLoggerLogo.png)
# MK Simple Logger [![Verified on Openbase](https://badges.openbase.com/js/verified/mk-simple-logger.svg)](https://openbase.com/js/mk-simple-logger?utm_source=embedded&utm_medium=badge&utm_campaign=verified-badge&utm_term=js/mk-simple-logger)
[![](https://data.jsdelivr.com/v1/package/npm/mk-simple-logger/badge)](https://www.jsdelivr.com/package/npm/mk-simple-logger) ![npm](https://img.shields.io/npm/v/mk-simple-logger?style=flat-square) ![npm](https://img.shields.io/npm/dw/mk-simple-logger?style=flat-square) ![Libraries.io dependency status for latest release](https://img.shields.io/librariesio/release/npm/mk-simple-logger?style=flat-square)


This a small package for node that allow implement a logger, and add same
config options, like theming or set custom format.
### ℹ️ℹ️ Since versión [0.1.11](http://gitea.marck-devs.com/marck/mk-simple-logger/src/tag/v0.1.11) the output is formatted for logger's name and log's level
# Install
The package is available in npm repositories.
npm install
```
npm install mk-simple-logger
```
yarn install
```
yarn add mk-simple-logger
```
## Usage

The main class is `SimpleLogger` so you need to import from the module

```js
const SimpleLogger = require("mk-simple-logger").SimpleLogger;
// or
const { SimpleLogger } = require('mk-simple-logger');
let logger = new SimpleLogger("mylogger");
logger.info("My Message");
// -> 20/02/2021 @ 20:10:40 - [ info ] - mylogger - My message

// allow string foramtting
logger.critical("My {p} message", { p: "custom" });
// -> 20/02/2021 @ 20:10:40 - [ CRITICAL ] - mylogger - My custom message
```

### Methods

```js
logger.debug("");
logger.info("");
logger.log("");
logger.warn("");
logger.error("");
logger.critical("");
```

### Set the log level

```js
SimpleLogger.setLogLevel("debug"); // 'warn'| 'info' | 'log' | 'error' |
'critical'
```

### Set custom format

The class allow change the date and the log line format:

```js
SimpleLogger.setFormat("{name} ==> {msg}");
// on log: mylogger ==> My message
SimpleLogger.setDateFormat("{y}/{month}/{day}");
// on write date: 21/03/14
```

Available log fields:

```
{name}  - logger name
{level} - loging level
{date}  - log date
{msg}   - log message
```

Available date format:

```
{day}     - current day
{weekDay} - day of the week
{month}   - the month
{year}    - the full year
{y}       - the short year
{hour}    - the hour
{min}     - the minuts
{sec}     - the seconds
{mil}     - the miliseconds
```

## Enable the file logger

SimpleLogger class can manger a file log too, this option need to set
the static option `isFile` as `true` and set the `logFile`:

```js
SimpleLogger.enableFileLog(); // enable file log
SimpleLogger.setFileLog("myapp.log"); //setting the file log
// if you want you can disable the stdout with:
SimpleLogger.disableStdout();
```

Logger can work with two options at the same time, they'r not restrictive.

# Load configuration from the env
Simple logger can load same data from the enviroment if it's set.
Can load:
- `LEVEL` as `logLevel`
- `LOG_FILE` for set the log's file
- `ERROR_FILE` for set the errors' file
- If `NODE_ENV` is set read it and set the log level from this env variable

# Global usage
It's posible to use the logger globally, for this prupose from version [`0.1.4`](http://gitea.marck-devs.com/marck/mk-simple-logger/src/tag/v0.1.4)
has the static method `global()` that return an static instance of the logger:
```js
	const {SimpleLogger} = require('mk-simple-logger');
	SimpleLogger.global().log('message'); // can access to all log method
	SimpleLogger.global().setName('name'); // can set the girglobal logger name
```
All methods all available for the glogal logger.

# Express integration
Sice version [`0.1.8` ](http://gitea.marck-devs.com/marck/mk-simple-logger/src/tag/v0.1.8) can use built logger for express applications:
```javascript
function logger(?name: string, ?level: string, ?format: string): ExpressMiddlewar
```

⚠️❗ Since [`0.1.9` ](http://gitea.marck-devs.com/marck/mk-simple-logger/src/tag/v0.1.9) you can set a custom format 
___

## Usage in express
```javascript
const express =  require('express');
const app = express();
const {SimpleLogger, logger} = require('mk-simple-logger');
app.use(express.json());
...
app.use(logger("SERVER", "info", "{method} {url} {status} {time}ms"));
...
app.listen(8080);

```
On request:
```
GET /uri1
logger:
18-02-22 @ 19:43:50 [ INFO ] -> SERVER -> GET /uri1 300 3ms
```
The logger work like the main logger so you can enable the file logger.
### Format
`Since 0.1.9`

The format param let you customize the message body, in this case, like the logger format, we have a prefixed mapped data:
<center>

| FIELD | Description	|
|------|----------------|
| {time} | Request time	|
| {method} | Request method	|
| {url} | Request url	|
| {status} | Response status code	|
</center>

You can play with this paramaters to generate a custom log mesage format.