Skip to main content

Sparkles Formatter Config

FormatterConfig defines how each log field is styled and formatted per platform.

type FormatterConfig = {
node?: NodeFormatterConfig;
browser?: BrowserFormatterConfig;
edge?: EdgeFormatterConfig;
};

All three platform configs share the same structure for most fields such as level, scope, and context, etc.
The only difference is: node includes additional fields like pid and hostname, which are only available in Node.js environments.

Example​

const logger = logry({
formatterConfig: {
node: {
timestamp: {
hide: false,
prefix: "<",
suffix: ">",
lineBreaks: 0,
spaceAfter: 1,
},
id: {
useAnsiStyle: true,
ansiStyle: "\x1b[35m",
},
level: {
enableAlignment: true,
},
scope: {
showOnlyLatest: true,
separator: " ➤ ",
},
message: {
customFormatter: ({ fieldValue, raw }) => {
const ansi = raw.level === "fatal" ? "\x1b[31m" : "";
return { fieldValue, withAnsiStyle: `${ansi}${fieldValue}\x1b[0m` };
},
},
meta: {
format: "pretty",
indent: 2,
},
context: {
depth: 2,
colors: true,
compact: true,
breakLength: 40,
},
},
browser: {
timestamp: { cssStyle: "border: 1px dashed red" },
message: {
customFormatter: ({ fieldValue, raw }) => {
const cssStyle = raw.level === "fatal" ? "color: red" : "";
return { fieldValue, cssStyle };
},
},
},
},
});

Platform FormatterConfig Fields​

FieldBase OptionsNode (additional)Browser (additional)
timestamp?customFormatteruseAnsiStyle, ansiStylecssStyle
id?customFormatteruseAnsiStyle, ansiStylecssStyle
level?customFormatter, enableAlignmentuseAnsiStyle, ansiStylecssStyle
scope?customFormatter, showOnlyLatest, separatoruseAnsiStyle, ansiStylecssStyle
message?customFormatteruseAnsiStyle, ansiStylecssStyle
meta?customFormatter, format, indentuseAnsiStyle, ansiStyle, InspectOptionscssStyle
context?customFormatter, format, indentuseAnsiStyle, ansiStyle, InspectOptionscssStyle
pid?customFormatteruseAnsiStyle, ansiStyle(N/A)
hostname?customFormatteruseAnsiStyle, ansiStyle(N/A)

Base Format Field Options​

These options are common to all formatter fields and control general display behaviors applicable to every field.

OptionType
hideboolean
prefixstring
suffixstring
lineBreaksnumber
spaceAfternumber

Return Format Differences​

While all platform formatters use the same config shape, they return slightly different structures depending on the runtime:

FieldOptions
node{ fieldValue, withAnsiStyle } (optional)
browser{ fieldValue, cssStyle } (optional)
edge{ fieldValue } (only)