# Logger

A collection of JS loggers for NOTHS apps:

* Middleware logger for Express servers (uses [winston](https://github.com/winstonjs/winston))
* Server-side logger (uses [winston](https://github.com/winstonjs/winston))
* Client-side logger

## Middleware

| Winston Transports | development | test | qa  | production |
| :----------------: | :---------: | :--: | :-: | :--------: |
|      Console       |     ✅      |  -   |  -  |     -      |
|        File        |      -      |  ✅  |  -  |     -      |
|      FluentD       |      -      |  -   | ✅  |     ✅     |
|       Sentry       |      -      |  -   |  -  |     -      |

### Example

```
import { MiddlewareLogger } from '@noths/logger'

const express = require('express')
const app = express()

const middlewareConfig = {
  service: 'MY-NEW-SERVICE', // required
  enviornment: 'development|test|qa|production' // optional - uses NODE_ENV or defaults to development
}

app.use(MiddlewareLogger(middlewareConfig))

app.get('/', function (req, res) {
  res.send('Hello World!')
})

app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
})
```

## Server

| Winston Transports | development | test | qa  | production |
| :----------------: | :---------: | :--: | :-: | :--------: |
|      Console       |     ✅      |  -   |  -  |     -      |
|        File        |      -      |  ✅  |  -  |     -      |
|      FluentD       |      -      |  -   | ✅  |     ✅     |
|       Sentry       |      -      |  -   |  -  |     ✅     |

### Example

```
import Logger from '@noths/logger'

const logger = new Logger({
  service: 'MY-NEW-SERVICE', // required
  sentry: { // required for production
    project: 'SENTRY_PROJECT_ID'
    keys: {
      public: 'SENTRY_PROJECT_PUBLIC_KEY',
      private: 'SENTRY_PROJECT_PRIVATE_KEY'
    }
  }
  enviornment: 'development|test|qa|production' // optional - uses NODE_ENV or defaults to development
});

const express = require('express')
const app = express()

app.get('/', function (req, res) {
  logger.info('Hello log land');
  res.send('Hello World!')
});

app.get('/whoops', function (req, res) {
  logger.error('Hello error land');
  res.status(500).send('Whoops!');
});

app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
})
```

## Client

| Logger  | development | test | qa  | production |
| :-----: | :---------: | :--: | :-: | :--------: |
| Console |     ✅      |  ✅  | ✅  |     -      |
| Sentry  |      -      |  -   |  -  |     ✅     |

### Setup

Add the Raven package client-side JS from your app.

```
<script src="https://cdn.ravenjs.com/3.16.1/raven.min.js" crossOrigin="anonymous" />
```

Call setup on the ClientLogger before any other JS in your app e.g.

```
import React from 'react';
import ReactDOM from 'react-dom';
import Logger from '@noths/logger';
import App from './components/App';

Logger.setup({
  environment: process.env.NODE_ENV, // <-- your responsibility
  clientKey: '123abc',
  projectId: '111111'
});

Logger.debug('Logging is up and running :)')
ReactDOM.render(<App />, document.getElementById('root'));
```

### API

#### `.setup({ environment, clientKey, projectId })`

* Does nothing in non-production environments.
* Sets up Sentry in production.

_Sentry credentials can be found at Sentry.io -> <PROJECT> -> Settings -> Keys -> DSN (Public)_

#### `.debug(message)`

#### `.info(message)`

#### `.error(message)`

## Logging levels

Logging levels for each logger can be found here: https://github.com/notonthehighstreet/tea/blob/91343602a2fadc875d28273eaa16db69dd20559c/documents/0004-js-logging-transports-and-levels.md#what-transports-and-logging-levels
