Skip to content

Commit

Permalink
feat: add logging (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
KirillDogadin-std authored Mar 28, 2023
1 parent 7cedbca commit 17fc322
Show file tree
Hide file tree
Showing 19 changed files with 2,814 additions and 1,814 deletions.
10 changes: 1 addition & 9 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,7 @@
],
"import/prefer-default-export": "off",
"no-console": [
"error",
{
"allow": [
"info",
"warn",
"trace",
"error"
]
}
"error"
]
}
}
Expand Down
34 changes: 34 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
### Logger usage

The project uses `pino` logger. This logger should be used over `console` calls.

For usual in-file logging one should import the logger from the corresponding `logger.ts` module
and use it in accordance with [documentation](https://github.com/pinojs/pino/tree/master/docs).

In case of logging the processes that happen during http request handling it is also
possible to use the dedicated logger that is accessible via the `express` request object.

Here's a couple of examples:

1. Http logging

```typescript
// some resolver code above
resolve: (_parent, _args, ctx) => {
ctx.apolloLogger.debug('resolver called');
});
},

```

2. Usual logger

In order to maintain the logging structure and be able to filter logs by file they have been produced at the following approach has to be taken:

```typescript
import { getModuleBinding, getChildLogger } from './logger';

const logger = getChildLogger({ msgPrefix: 'PREFIX' }, { myCustomBinding: 'Funny guy' });
```

If this approach is used - then it will be possible to filter the logs via providing the corresponding environment variable values. For more on this read the root [README](./README.md)
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ npm i
# Run application in development mode
npm run dev

# Run application with extensive logging enabled (full ORM logging + app's logger has `debug` level on)
npm run debug

# Typechecking (via TypeScript / tsc)
npm run typecheck

Expand All @@ -32,6 +35,10 @@ npm run lint
npm run test
```

### Logging configuration

The configuration is received from the `logger.config.ts` file at the root of the project. Adjust the file parameters to control the logger behaviour.

### Coding Setup

To install the correct node version, we recommend that you use [nvm](https://github.com/nvm-sh/nvm). If you have `nvm` installed you can run:
Expand All @@ -56,6 +63,7 @@ cp developer.env .env
- `PORT` (optional, default: 3000): port on which the server will run.
- `AUTH_SIGNUP_ENABLED` (optional, default: `false`): if signing up mutation is allowed (i.e. user creation via endpoint is enabled)
- `JWT_EXPIRATION_PERIOD` (optional, default: `'7d'`): how soon the signed jwt token will expire.
- `DEBUG` (optional): if set, enables the different more explicit logging mode where debug levels are set to `debug` for the app's logger and `query` for db logger

### Project-Requirements

Expand Down
35 changes: 35 additions & 0 deletions logger.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { LoggerConfig } from './src/types';

export const defaultLoggerConfig: LoggerConfig = {
// Filter by module name
moduleFilter: [],
// Filter by log prefix, e.g. adding
// `pref` to filter is going to correspond to showing `[PREF] ` logs
prefixFilter: [],
// Lowest printed log level of default logger
logLevel: 'info',
// Log levels of prisma logger that are enabled
dbLogLevel: ['info'],
// Lowest printed log level of express logger
httpLogLevel: 'info',
};

export const debugLogConfig: LoggerConfig = {
// Filter by module name
moduleFilter: [],
// Filter by log prefix, e.g. adding
// `pref` to filter is going to correspond to showing `[PERF]` logs
prefixFilter: [],
// Lowest printed log level of default logger
logLevel: 'debug',
// Log levels of prisma logger that are enabled
dbLogLevel: ['query'],
// Lowest printed log level of express logger
httpLogLevel: 'debug',
};

function getLoggerConfig(debug: boolean): LoggerConfig {
return (debug ? debugLogConfig : defaultLoggerConfig);
}

export const loggerConfig = getLoggerConfig(!!process.env.DEBUG);
Loading

0 comments on commit 17fc322

Please sign in to comment.