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

Document how to set "undef_column_value" #96

Open
Laz80UK opened this issue Mar 21, 2019 · 7 comments
Open

Document how to set "undef_column_value" #96

Laz80UK opened this issue Mar 21, 2019 · 7 comments

Comments

@Laz80UK
Copy link

Laz80UK commented Mar 21, 2019

Hi,

I couldn't find any documentation on how to set the "undef_column_value".
It causes problems when trying to convert the output to JSON as my expected field type is an integer which could be NULL if not available, however Log::Log4perl::Layout::PatternLayout converts everything that's undef to a string, i.e. "[undef]".
Many thanks.

@mohawk2
Copy link
Collaborator

mohawk2 commented Sep 6, 2020

A quick git grep shows me it's used in Log::Log4perl::Layout::PatternLayout, and its value from Log::Log4perl::Appender::DBI always appears to be undef. I think this needs a bit of docs and testing (and maybe fixing)! Want to help out?

@Laz80UK
Copy link
Author

Laz80UK commented Sep 7, 2020

Kinda busy here, but trying my best...
I documented and suggested a workaround here:
mschout/Log-Log4perl-Layout-JSON#6

@mohawk2
Copy link
Collaborator

mohawk2 commented Sep 15, 2020

This seems to me like an undef is getting prematurely coerced into a string, which isn't always appropriate as you identify. My feeling is that it should be left as-is, or at least be overridable to a real undef. I'm very open to a backwards-compatible PR implementing this! (with tests and docs, naturally)

@abraxxa
Copy link
Contributor

abraxxa commented Nov 3, 2021

We'd need this to be able to prefix the message by the NDC which should default to an empty string in case there is no NDC set.

@abraxxa
Copy link
Contributor

abraxxa commented Nov 3, 2021

My search for a workaround showed that NDC doesn't care about this config option: https://metacpan.org/dist/Log-Log4perl/source/lib/Log/Log4perl/NDC.pm#L19

@abraxxa
Copy link
Contributor

abraxxa commented Nov 3, 2021

This is what I've come up with:

use Log::Log4perl;
use Log::Log4perl::Layout::PatternLayout;
$Log::Log4perl::ALLOW_CODE_IN_CONFIG_FILE = 1;
Log::Log4perl::Layout::PatternLayout::add_global_cspec('U', sub {
    my $ndc = Log::Log4perl::NDC->get();
    return $ndc eq '[undef]'
        ? ''
        : "$ndc ";
});
$Log::Log4perl::ALLOW_CODE_IN_CONFIG_FILE = 0;

And use %U instead of %x, with no whitespace to the next placeholder, which is %m in my case.
I haven't decided if that's ugly or genius...

@mohawk2
Copy link
Collaborator

mohawk2 commented Jun 1, 2022

@abraxxa Doc patches welcome? :-)

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

No branches or pull requests

3 participants