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

util.format options need #1139

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

thegobot
Copy link

options to config...
Can't change depth now

function colouredLayout(loggingEvent) {
return timestampLevelAndCategory(loggingEvent, loggingEvent.level.colour) + util.format(...loggingEvent.data);
function colouredLayout(loggingEvent) {
return timestampLevelAndCategory(loggingEvent, loggingEvent.level.colour) + util.formatWithOptions({depth: 4}, ...loggingEvent.data);
Copy link
Contributor

@lamweili lamweili Jan 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thegobot Your changes don't make it able to be changed, or am I missing something?

It would still not work for people requiring more depths.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that your logger doesn't print objects with depth > 2 (default in util.inspect depth = 2)
I believe that you need to move the settings of formatWithOptions in the config

logger.debug(util.formatWithOptions({ depth: 5 }, myObject));

I understand, but it's not convenient

@lamweili
Copy link
Contributor

lamweili commented Jan 15, 2022

@thegobot Would using JSON.stringify(myObject, null, 2) manually suffice?

Using your changes (hard-coded to depth=4)

const myObject = {"a":"a","b":{"c":"c","d":{"e":"e","f":{"g":"g","h":{"i":"i","j":{"k":"k"}}}}}};

const log4js = require("log4js");
const logger = log4js.getLogger();
logger.level = "debug";
logger.debug(myObject);
[2022-01-16T02:30:16.495] [DEBUG] default - {
  a: 'a',
  b: {
    c: 'c',
    d: { e: 'e', f: { g: 'g', h: { i: 'i', j: [Object] } } }
  }
}

Using original code

const myObject = {"a":"a","b":{"c":"c","d":{"e":"e","f":{"g":"g","h":{"i":"i","j":{"k":"k"}}}}}};

const log4js = require("log4js");
const logger = log4js.getLogger();
logger.level = "debug";
logger.debug(myObject);
logger.debug(JSON.stringify(myObject, null, 2));              // manual

const util = require("util");
logger.debug(util.formatWithOptions({ depth: 5 }, myObject)); // manual
[2022-01-16T02:24:44.084] [DEBUG] default - { a: 'a', b: { c: 'c', d: { e: 'e', f: [Object] } } }
[2022-01-16T02:23:54.991] [DEBUG] default - {
  "a": "a",
  "b": {
    "c": "c",
    "d": {
      "e": "e",
      "f": {
        "g": "g",
        "h": {
          "i": "i"
          "j": {
            "k": "k"
          }
        }
      }
    }
  }
}
[2022-01-16T21:31:13.805] [DEBUG] default - {
  a: 'a',
  b: {
    c: 'c',
    d: {
      e: 'e',
      f: { g: 'g', h: { i: 'i', j: { k: 'k' } } }
    }
  }
}

@lamweili lamweili linked an issue Jan 20, 2022 that may be closed by this pull request
@lamweili lamweili linked an issue Jan 27, 2022 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Question] When printing an array, how to print all the items? Add the ability to provide inspection options
2 participants