%x
is explicitly documented as returning [undef]
if nothing is on the NDC stack. So with standard %x
you're out of luck. However, by using a self-defined placeholder and looking into the NDC internals you can format the output like you like, e.g.:
Log::Log4perl::Layout::PatternLayout::add_global_cspec('U', sub {
@Log::Log4perl::NDC::NDC_STACK ? join(" ", @Log::Log4perl::NDC::NDC_STACK) . " " : ''
});
Then simply use
Log::Log4perl->easy_init({ level => $INFO, layout => "%U%m%n" });
(note the missing space after %U here).
It would be better if the current NDC_STACK was available by function, or if the existing variables was documented. Maybe ask the log4perl developers if they could do any of both?