Skip to content

Commit

Permalink
feat: add logging destination option (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
resure authored Aug 23, 2023
1 parent 45c7c54 commit 833b5b6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
6 changes: 5 additions & 1 deletion docs/contexts.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ childCtx.log('Waiting for response to prepare');
// ...
```
**Note: NodeKit uses pino for logging but does not exposes any of it's API or configuration capabilities. However, we would like to add option for configuring pino little later, see [this issue](https://github.com/gravity-ui/nodekit/issues/5) for more details**.
**Note: NodeKit uses pino for logging but does not exposes most of it's API or configuration capabilities. However, we would like to add option for configuring pino little later, see [this issue](https://github.com/gravity-ui/nodekit/issues/5) for more details**.
### Logging destinations
You can override default logs destination (stdout) with `appLoggingDestination` option which accepts [pino.Destination](https://github.com/pinojs/pino/blob/master/docs/api.md#destination). It allows to implement custom formatters for logs as well as custom transports for them. Currently, this option only works if devMode is set to false. If you have an usecase when you need this option alongside enabled dev mode, feel free to open an issue.
## Distributed tracing
Expand Down
13 changes: 10 additions & 3 deletions src/lib/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import pino from 'pino';
interface InitLoggerOptions {
appName: string;
devMode: boolean;
destination?: pino.DestinationStream;
}

export function initLogger({appName, devMode}: InitLoggerOptions) {
export function initLogger({appName, devMode, destination}: InitLoggerOptions) {
const transportConfig = devMode
? {
target: 'pino-pretty',
Expand All @@ -17,13 +18,19 @@ export function initLogger({appName, devMode}: InitLoggerOptions) {
}
: undefined;

return pino({
const options = {
name: appName,
safe: true,
leve: 'debug',
serializers: {
error: pino.stdSerializers.err,
},
transport: transportConfig,
});
};

if (destination && !devMode) {
return pino(options, destination);
} else {
return pino(options);
}
}
1 change: 1 addition & 0 deletions src/nodekit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export class NodeKit {
this.logger = initLogger({
appName: this.config.appName as string,
devMode: appDevMode,
destination: this.config.appLoggingDestination,
});

const redactSensitiveQueryParams = prepareSensitiveQueryParamsRedacter(
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {pino} from 'pino';

export interface AppConfig {
appName?: string;
appVersion?: string;
Expand All @@ -11,11 +13,14 @@ export interface AppConfig {
nkDefaultSensitiveHeaders?: string[];
nkDefaultHeadersWithSensitiveUrls?: string[];
nkDefaultSensitiveQueryParams?: string[];

appSensitiveKeys?: string[];
appSensitiveHeaders?: string[];
appHeadersWithSensitiveUrls?: string[];
appSensitiveQueryParams?: string[];

appLoggingDestination?: pino.DestinationStream;

appTracingEnabled?: boolean;
appTracingServiceName?: string;
appTracingDebugLogging?: boolean;
Expand Down

0 comments on commit 833b5b6

Please sign in to comment.