Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: add logging #21

Merged
merged 38 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
fe0187d
add logging
KirillDogadin-std Mar 16, 2023
cc3845a
capitalize msg in logger
KirillDogadin-std Mar 16, 2023
561445e
prisma db logging
KirillDogadin-std Mar 16, 2023
1fabb55
adjust check for proper db log levels
KirillDogadin-std Mar 16, 2023
0277a0b
adjust readme with env var
KirillDogadin-std Mar 16, 2023
af86c86
log responses
KirillDogadin-std Mar 16, 2023
118c055
async
KirillDogadin-std Mar 16, 2023
d49b8bd
filter functionality
KirillDogadin-std Mar 16, 2023
1b63827
improve md files
KirillDogadin-std Mar 16, 2023
9d0442e
improve logging filter
KirillDogadin-std Mar 16, 2023
856ed6f
doc update
KirillDogadin-std Mar 16, 2023
9a4fe25
split code
KirillDogadin-std Mar 20, 2023
367532e
refactor logging
KirillDogadin-std Mar 21, 2023
12ca9d6
allow no args for child logger
KirillDogadin-std Mar 21, 2023
8bc37ee
integrate logger into apollo
KirillDogadin-std Mar 21, 2023
deeb411
unused deps
KirillDogadin-std Mar 21, 2023
0b97e87
readme adjust
KirillDogadin-std Mar 21, 2023
df042dc
debug command
KirillDogadin-std Mar 21, 2023
d41337b
type package
KirillDogadin-std Mar 21, 2023
3d484ec
lint
KirillDogadin-std Mar 21, 2023
e0a0352
lint
KirillDogadin-std Mar 21, 2023
690bd91
lint
KirillDogadin-std Mar 21, 2023
9c40e07
nits
KirillDogadin-std Mar 22, 2023
d33fb4a
Merge branch 'main' into add_logging
KirillDogadin-std Mar 22, 2023
3aa9dae
merge
KirillDogadin-std Mar 22, 2023
157f413
lint
KirillDogadin-std Mar 22, 2023
5c6a3ca
typecheck
KirillDogadin-std Mar 22, 2023
e2daa00
minor
KirillDogadin-std Mar 23, 2023
48c3264
lint
KirillDogadin-std Mar 23, 2023
2562aad
update readme
KirillDogadin-std Mar 23, 2023
e2d8fc0
make prisma log things via pino
KirillDogadin-std Mar 23, 2023
4e205e7
adjust types
KirillDogadin-std Mar 23, 2023
4caa41c
readme and minors
KirillDogadin-std Mar 23, 2023
e9fc986
Merge branch 'main' into add_logging
KirillDogadin-std Mar 27, 2023
76e73ed
tests added
KirillDogadin-std Mar 27, 2023
6cee018
added the comment with doc link
KirillDogadin-std Mar 27, 2023
58850a4
rm block for ending api
KirillDogadin-std Mar 28, 2023
47d6fdd
adjust tests
KirillDogadin-std Mar 28, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
valiafetisov marked this conversation as resolved.
Show resolved Hide resolved
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