Skip to content

[Feature Request]: Better Handling Of JSON in simple() formatter #346

@klondikemarlen

Description

@klondikemarlen

The vision

Context

Certain types of log messages show up as [object Object]

e.g.

info: [object Object] {"timestamp":"2025-12-24T17:23:23.442Z"}
info: applied 1 migrations. {"event":"up","timestamp":"2025-12-24T17:23:23.443Z"}

Proposed Solution

Simple formatter with object printing.

import { format } from "winston"
import { has } from "lodash"

import { MESSAGE } from "triple-beam"
import stringify from "safe-stable-stringify"
import { type TransformableInfo } from "logform"

/**
 * Winston's (Logform's) simple formatter, but stringifies object messages instead of producing "[object Object]".
 *
 * See https://github.com/winstonjs/logform/blob/18795bc185d5eb485fb0289d08c1e220a6d3e44c/simple.js
 */
export const simpleWithObjectHandling = format((info: TransformableInfo) => {
  const stringifiedRest = stringify(
    Object.assign({}, info, {
      level: undefined,
      message: undefined,
      splat: undefined,
    })
  )

  let message = info.message
  if (typeof info.message === "object" && `${message}` === "[object Object]") {
    message = stringify(info.message)
  }

  let padding = ""
  if (has(info, "padding") && has(info.padding, info.level)) {
    padding = info.padding[info.level]
  }

  if (stringifiedRest !== "{}") {
    info[MESSAGE] = `${info.level}:${padding} ${message} ${stringifiedRest}`
  } else {
    info[MESSAGE] = `${info.level}:${padding} ${message}`
  }

  return info
})

export default simpleWithObjectHandling

Produces

info: {"durationSeconds":0.235,"event":"migrated","name":"2025.12.04T08.57.45.create-building-expense-categories-table.ts"} {"timestamp":"2025-12-24T17:24:31.156Z"}
info: applied 1 migrations. {"event":"up","timestamp":"2025-12-24T17:24:31.156Z"}

Use case

icefoganalytics/elcc-data-management@1018a12

🔎 Search Terms

simple; [object Object]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions