Skip to content

Conversation

@l0ner
Copy link
Contributor

@l0ner l0ner commented Aug 27, 2022

Added to patternLayout the following fields:

  • %M - function name of the caller issuing the logging request
  • %C - class name of the caller issuing the logging request
  • %A - function alias of the caller issuing the logging request

The first two come from log4j, and use the same field specifiers.

When called from method bar of a class Foo:

  • %M will be replaced with bar
  • %C will be replaced with Foo
  • %A will be empty

When called from function foo:

  • %M will be replaced with foo
  • %C will be empty
  • %A will be empty

%A will be non empty only if the call stack parsed to obtain the values contains string [as foo].

This is the same behaviour as log4j (except for the %A).

Advise: would it make sense to also include something like %F for obtaining the full, unsplitted name?


Added table for clarity:

Error Stacktrace %M
function name
%C
class name
%A
alias name
%F
caller name
<empty>
foo foo foo
foo [as bar] foo bar foo [as bar]
Foo.bar bar Foo Foo.bar
Foo.bar [as baz] bar Foo baz Foo.bar [as baz]

Added to patternLayout the following fields:

- %M - function name of the caller issuing the logging request
- %C - class name of the caller issuing the logging request
- %A - function alias of the caller issuing the logging request

The first two come from log4j, and use the same field specifiers.

When called from method bar of a Class foo:

- %M will be replaced with bar
- %C will be replaced with Foo
- %A will be empty

When called from function foo:

- %M will be replaced with foo
- %C will be empty
- %A will be empty

%A will be non empty only if the call stack parsed to obtain the values
contains string [as foo].
@lamweili
Copy link
Contributor

Advise: would it make sense to also include something like %F for obtaining the full, unsplitted name?

Can you provide a sample for my understanding? 🤗

@l0ner
Copy link
Contributor Author

l0ner commented Aug 28, 2022

Advise: would it make sense to also include something like %F for obtaining the full, unsplitted name?

Can you provide a sample for my understanding? 🤗

The names come from splitting an input string, that comes from the call stack. This string can be anything between:

  • empty (when the caller is in a file scope or an anonymous function in a file scope)
  • foo - caller is a function foo
  • foo [as bar] - caller is a function foo imported as bar
  • Foo.bar - caller is method bar of a class Foo
  • Foo.bar [as baz] - caller is method bar of a class Foo imported as baz

Somehing like %F (caller name) would be replaced by one of this possibile strings. Could prove useful when user wants fully qualified caller name, insted of wanting only function/class/alias name.

@lamweili
Copy link
Contributor

I see. So %F would be the entire group of the regexp?
I think that's a good idea 👍, so there is no need to do concatenation manually.

You must be careful to ensure the test cases have good coverage for the various permutations/combinations for the match.

@l0ner
Copy link
Contributor Author

l0ner commented Aug 29, 2022

I have added the %F field containing fully qualified caller name.

Test should be good, but please review, I've tried my best to add all possible permutations.

Copy link
Contributor

@lamweili lamweili left a comment

Choose a reason for hiding this comment

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

Hi, I made a review and have some comments.
Could you please address them? 🙏

@lamweili
Copy link
Contributor

lamweili commented Sep 1, 2022

Anyway, I have also edited your original post with a table for clarity for whoever might be reading in future.
Feel free to correct it if it's wrong.

@l0ner
Copy link
Contributor Author

l0ner commented Sep 1, 2022

Anyway, I have also edited your original post with a table for clarity. Feel free to correct it if it's wrong.

It's good. No need for changes.

Co-authored-by: Lam Wei Li <peteriman@mail.com>
@lamweili lamweili force-pushed the patternLayout-class-function-alias-names branch from 6c214ca to dd3051d Compare September 1, 2022 11:29
@lamweili lamweili added the enhancement New feature or request label Sep 1, 2022
@lamweili lamweili added this to the 6.6.2 milestone Sep 1, 2022
@lamweili lamweili merged commit be433f2 into log4js-node:master Sep 1, 2022
@lamweili lamweili modified the milestone: 6.7.0 Sep 1, 2022
@l0ner
Copy link
Contributor Author

l0ner commented Sep 1, 2022

Great to see this merged! Can't wait the see this available on npm so I can use it in my work.

lamweili added a commit that referenced this pull request Sep 1, 2022
lamweili added a commit that referenced this pull request Sep 1, 2022
refactor(#1316): code flow and readability (%C:class, %M:function, %A:alias, %F:full - in order)
@vineetmyhq
Copy link

When are we expecting a release for this?
Really need this feature when using logger in a wrapper, where can't track who generated the log.

@lamweili
Copy link
Contributor

Target by end of the week. 🤗

@lamweili
Copy link
Contributor

lamweili commented Oct 1, 2022

Released in log4js@6.7.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants