npm package: @sumor-cloud/logger
Description: Sumor Logger aims to provide a universal logging format, color differentiation, cross-time zone, multi-language, and highly scalable logging interface.
Latest version: 1.0.5
Deprecated: outdated

# @sumor-cloud/logger
Sumor Logger aims to provide a universal logging format, color differentiation, cross-time zone, multi-language, and highly scalable logging interface.

[![NPM version](https://img.shields.io/npm/v/%40sumor-cloud%2Flogger.svg)](https://www.npmjs.com/package/@sumor-cloud/logger)
[![NPM downloads](https://img.shields.io/npm/dm/%40sumor-cloud%2Flogger.svg)](https://www.npmjs.com/package/@sumor-cloud/logger)

A [Sumor Cloud](https://www.sumor.com) Tool.  
[More Documentation](https://www.sumor.com/help)

## Installation

```shell
npm install -D @sumor-cloud/logger
```

## Usage

### General Usage

```js
import Logger from '@sumor-cloud/logger';
const logger = new Logger();

logger.trace("Hello World!");
// You will see the following output:
// 2020-01-01 00:00:00.000 TRACE MAIN - Hello World!
```

### Change Scope
For some case, we need categorize logs. `scope` is used for this purpose.
```js
import Logger from '@sumor-cloud/logger';
const logger = new Logger({
    scope: 'DEMO'
});
logger.trace("Hello World!");
// You will see the following output:
// 2020-01-01 00:00:00.000 TRACE DEMO - Hello World!
```

### Identifier User
For some case, we need identifier user. `id` is used for this purpose.
```js
import Logger from '@sumor-cloud/logger';
const logger = new Logger({
    id: 'USER001'
});
logger.trace("Hello World!");
// You will see the following output:
// 2020-01-01 00:00:00.000 TRACE MAIN USER001 - Hello World!
```

### Change Level
Most of the time, we only need to output logs of a certain level. Then we can decide if store and display it or not.
```js
import Logger from '@sumor-cloud/logger';
const logger = new Logger();
logger.trace("Hello World!"); // trace is the lowest level, all logs will be output
logger.debug("Hello World!");
logger.info("Hello World!");
logger.warn("Hello World!");
logger.error("Hello World!");
logger.fatal("Hello World!"); // fatal is the highest level, only critical error will be output
```

### Enable Translation
```js
import Logger from '@sumor-cloud/logger';
const logger = new Logger({
    i18n:{
        "SAY_HELLO":"Hello, {user}"
    }
});
logger.info("SAY_HELLO",{user:"Sumor"});
// You will see the following output:
// 2020-01-01 00:00:00.000 INFO MAIN - Hello, Sumor
```

### Change Timezone
```js
import Logger from '@sumor-cloud/logger';
const logger1 = new Logger({
    offset: 2 * 60 // UTC+2 offset is 2 hours
});
logger1.info("Hello World!");
// You will see the following output:
// 2020-01-01 02:00:00.000 INFO MAIN - Hello World!

const logger2 = new Logger({
    offset: 8 * 60 // UTC+8 offset is 8 hours
});
logger2.info("Hello World!");
// You will see the following output:
// 2020-01-01 08:00:00.000 INFO MAIN - Hello World!

```

### Save Logs

Usually, we need to save logs to a file. We can add saver to the logger.
```js
import Logger from '@sumor-cloud/logger';
import fs from 'fs';
const logger = new Logger({
    saver: (message) => {
        // save message to file
        fs.appendFileSync('main.log', message);
    }
});
```

### Load Logs

Sometimes, you may need to load logs from a file. We can use load method to resume the logs.
And also, you can use this to switch log language as well by passing different i18n.
```js
import Logger from '@sumor-cloud/logger';
const logger = new Logger({
    i18n:{
        "SAY_HELLO":"Morning, {user}"
    }
});
logger.load(`# [2020-01-01T00:00:00.000Z] [INFO] [MAIN] - SAY_HELLO | {"user":"Sumor"}`);
// You will see the following output:
// 2020-01-01 00:00:00.000 INFO MAIN - Morning, Sumor
```


## Contributing & Issues

You can use this tool for free.  
But we are a commercial company, to ensure stability, we currently don't offer a joint contribution mechanism.  
You can report issues through [Report Library Issue Ticket](https://www.sumor.com/ticket/library), and we will fix them as soon as possible.

## License

In consideration of the chaotic situation in the open source community, we provide obfuscated code for use and security analysis.  
However, we do not provide the source code in its original form in order to protect the ongoing maintenance and creative interests and motivations.  
The released code in [NPMJS](https://www.npmjs.com) is based on MIT. The source code is closed-source.

Our tools are free to use in order to help build cloud applications more efficiently. Our commercial purpose is to improve the ecosystem of business websites.  
If there are any innovations that greatly benefit the open source community, it will be separated and contributed to the open source community.