npm package: @lahaus-nuxt/winston-logs
Description: My great Nuxt.js project
Author: Alver Alexander Grisales Ortega
Latest version: 0.7.0

# Welcome to @lahaus-nuxt/winston-logs 👋

[![Version](https://img.shields.io/npm/v/@lahaus-nuxt/winston-logs.svg)](https://www.npmjs.com/package/@lahaus-nuxt/winston-logs)
[![Documentation](https://img.shields.io/badge/documentation-yes-brightgreen.svg)](https://github.com/la-haus/nuxt-winston-logs#readme)
[![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://github.com/la-haus/nuxt-winston-logs/graphs/commit-activity)

Nuxt module to add winston/logging to your application by capturing requests passing through the server-middleware using the http module in nodejs.
This module is only compatible with nuxtjs version 2.

### 🏠 [Homepage](https://github.com/la-haus/nuxt-winston-logs#readme)

## Installation

1. Install npm package

```sh
yarn add @lahaus-nuxt/winston-logs # or npm i @lahaus-nuxt/winston-logs
```

2. Install winston

```sh
yarn add winston # or  npm i winston
```

3. Edit your `nuxt.config.js` file to add module

```js
{
  modules: ['@lahaus-nuxt/winston-logs'];
}
```

4. Change the options using the `winstonLogConfig` key or by adding a second parameter in the module configuration. Refer to the Usage section for details.

# Usage

1. By default, `@lahaus-nuxt/winston-logs` exposes some basic options for common needs. Internally, these options are used to create a basic [winston logger instance](https://github.com/winstonjs/winston#usage) and wire up middleware.

   The default values are:

   ```js
   // ...
   {
     // Active error request middleware handler.
     addErrorMiddleware: true,
     // Active error request middleware handler.
     addRequestMiddleware: true
     // activate the module
     enabled: true,
     // Set the logger options.
     loggerOptions: {
       level: 'info',
       exitOnError: false,
       format: combine(timestamp(), errors({ stack: true }), json()),
       transports: [new transports.Console()],
     },
     ...yourOverrides
   }
   // ...
   ```

With options module

```js
{
  modules: ['@lahaus-nuxt/winston-logs', { enabled: true }];
}
```

With `winstonLogConfig` key

```js
{
  modules: ['@lahaus-nuxt/winston-logs'],
  winstonLogConfig: {
    enabled: true
  }
}
```

2. To access the winston instance from within Nuxt lifecycle areas, use the `$winstonLog` key from the Nuxt context object. Note: This is only available for server-side executions. For example, because asyncData is an isomorphic function in Nuxt, you will need to guard `$winstonLog` access with something like if (process.server) { ... }

Example `nuxtServerInit` in your apps `~/store/index.js:`

```js
// ...
export const actions = {
  async nuxtServerInit({ store, commit }, { req, $ddTrace }) {
    $winstonLog.info({ message: 'hello word' });
  },
};
// ...
```

Example `asyncData` in your apps `~/pages/somepage.vue`:

```js
    // ...
    asyncData(context) {
      if (process.server) {
        context.$winstonLog.info({ message: 'hello word' });
      }
    }
    // ...
```

For more information on winston options, see the package [documentation](https://github.com/winstonjs/winston#quick-start).

# Using `process.winstonLog` in a Nuxt Module

Because modules are executed sequentially, any additional [Nuxt modules](https://nuxtjs.org/docs/2.x/directory-structure/modules#write-your-own-module) should be loaded _after the `@lahaus-nuxt/winston-logs` module_. You can then access the winston logger instance via `process.winstonLog` as needed.

# Important

The datadog-trace module uses interceptors to trace network requests and database operations in the application. By adding the winston-logs module after datadog-trace, you ensure that additional logs are enriched with information traced by datadog, such as the service name, environment, and version.

If you add the Winston module before datadog-trace, it is possible that the additional logs will not be enriched with the information traced by datadog. This can make it difficult to diagnose problems in the application and limit the effectiveness of the logs.
For example:

✅ Correct

```typescript
modules: ['@lahaus-nuxt/datadog-trace', '@lahaus-nuxt/winston-logs'];
```

❌ Incorrect

```typescript
modules: ['@lahaus-nuxt/winston-logs', '@lahaus-nuxt/datadog-trace'];
```

## Author

👤 **Alver Alexander Grisales Ortega <alvergrisales@lahaus.com>**

---

_This README was generated with ❤️ by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_

_This project generated by [create-nuxt-module](https://github.com/potato4d/create-nuxt-module)_