npm package: @gtomato-web/logger
Description: A simple logger library
Author: GTomato Web
Latest version: 1.1.10
Deprecated: Unmaintained

# Logger

A simple logger library

## Table of Contents

- [Installation](#installation)
- [Usage](#usage)

## Installation

Install the package as a dev dependency:

### NPM

```shell
$ npm install -D @gtomato-web/logger
```

### Yarn

```shell
$ yarn add -D @gtomato-web/logger
```

## Usage

### Init Options

| Option    | Description                                                                 | Type                          | Optional? | Default                     |
|-----------|-----------------------------------------------------------------------------|-------------------------------|-----------|-----------------------------|
| env       | Runtime environment,<br/>Logger will only active in development environment | "development" \| "production" | Y         | NODE_ENV \|\| "development" |
| level     | Log level,<br/>Log type that with lower log level will be disabled          | See [Log levels](#log-levels) | Y         | "log"                       |
| namespace | The namespace that included in logs                                         | string                        | Y         | undefined                   |

### Log levels

```typescript
// options.level accept both string and number e.g. 0 or 'log' (Which is the same) or etc...
const levels = {
  log: 0,
  info: 1,
  warn: 2,
  error: 3,
}
```

### Init the logger

```typescript
// String as the init options
// env will be NODE_ENV or 'development' if NODE_ENV is not available
// level will be 'log' if not specified
const loggerInitWithString = gtwLogger.init('somefile.ts');

// or 

// Object as the init options
const LoggerInitWithOptions = gtwLogger.init({
  env: 'development',
  level: 'info', // So that LoggerInitWithOptions.log will be disabled 
  namespace: 'somefile.ts',
});
```

### Example Usage

#### Common usage

```typescript
import gtwLogger from '@gtomato-web/logger';

const logger = gtwLogger.init('someFile.ts');

// ...Somewhere between your code
// ...
logger.log('This log a basic log message');
//=> LOG [someFile.ts] This log a basic log message

logger.info('This log a information message');
//=> INFO [someFile.ts] This log a information message

logger.warn('This log a warning message');
//=> WARN [someFile.ts] This log a warning message

logger.error('This log an error message');
//=> ERROR [someFile.ts] This log an error message
// ...
// ...Continue your code
```

#### Updating the log level

```typescript
import gtwLogger from '@gtomato-web/logger';

const logger = gtwLogger.init({namespace: 'anotherFile.ts'});

// ...Somewhere between your code
// ...
logger.error('This log an error message');
//=> ERROR [anotherFile.ts] This log an error message

logger.warn('This log a warning message');
//=> WARN [anotherFile.ts] This log a warning message

logger.level = 'warn';

logger.info('This log a information message');
// SKIPPED

logger.level = 'log';

logger.log('This log a basic log message');
//=> LOG [anotherFile.ts] This log a basic log message
// ...
// ...Continue your code
```

You may also update th env and namespace in the same way

---

[^ Back to top](#logger)