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 destination option #22

Merged
merged 1 commit into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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