Skip to content
Open
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
9 changes: 9 additions & 0 deletions .changeset/gold-dragons-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@seek/logger': minor
---

feat: Add configurable string length and function handling options

- Add `stringLength` option to control maximum loggable string length (default: 512)
- Add `functions` option to control whether functions are included in log objects (default: true)
- deps: dtrim ^1.13.0
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
},
"dependencies": {
"@pinojs/redact": "^0.4.0",
"dtrim": "^1.11.0",
"dtrim": "^1.13.0",
"pino": "^10.1.0",
"pino-std-serializers": "^7.0.0"
},
Expand Down
180 changes: 78 additions & 102 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions src/formatters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,53 @@ import type { LoggerOptions } from 'pino';

export const DEFAULT_MAX_OBJECT_DEPTH = 4;

export const DEFAULT_OMIT_FUNCTIONS = false;
export const DEFAULT_STRING_LENGTH = 512;

export interface FormatterOptions {
/**
* Maximum property depth of objects being logged. Default: 4
*/
maxObjectDepth?: number;

/**
* Controls how function properties are handled in log objects.
*
* When `true`: Function properties are omitted from log output entirely.
*
* When `false`: Function properties are represented as `"[Function]"` in logs. (default)
*
* @example
* ```typescript
* // omitFunctions: false
* logger.info({ fn: () => {}, data: 'value' });
* // Output: { "fn": "[Function]", "data": "value" }
*
* // omitFunctions: true
* logger.info({ fn: () => {}, data: 'value' });
* // Output: { "data": "value" }
* ```
*/
omitFunctions?: boolean;

/**
* This allows finer control of redaction by providing access to the full text.
*/
redactText?: (input: string, redactionPlaceholder: string) => string;

/**
* Maximum length loggable string length. Default: 512
*/
maxStringLength?: number;
}

export const createFormatters = (
opts: FormatterOptions & Required<Pick<LoggerOptions, 'serializers'>>,
): LoggerOptions['formatters'] => {
const trim = trimmer({
depth: opts.maxObjectDepth ?? DEFAULT_MAX_OBJECT_DEPTH,
functions: !(opts.omitFunctions ?? DEFAULT_OMIT_FUNCTIONS),
string: opts.maxStringLength ?? DEFAULT_STRING_LENGTH,
retain: new Set(Object.keys(opts.serializers)),
});

Expand Down
Loading