0

I am running an application that logs things using Log4j2. I am configuring the logging programmically using the code below:

  ConfigurationBuilder<BuiltConfiguration> builder  = ConfigurationBuilderFactory.newConfigurationBuilder();
  
  AppenderComponentBuilder console = builder.newAppender("stdout","Console");
  builder.add(console);
  
  FilterComponentBuilder flow = builder.newFilter("MarkerFilter",Filter.Result.ACCEPT,Filter.Result.DENY);
  flow.addAttribute("marker","FLOW");
  
  console.add(flow);
  
  LayoutComponentBuilder layout = builder.newLayout("PatternLayout");
  standard.addAttribute("pattern","%d [%-6p] %c{1} - %m%n");

  console.add(standard);
  
  RootLoggerComponentBuilder rootLogger = builder.newRootLogger(Level.DEBUG);
  rootLogger.add(builder.newAppenderRef("stdout"));
  
  builder.add(rootLogger);
  
  Configurator.initialize(builder.build());    

Please note that I am attempting to set a pattern using the addAttribute() method of the LayoutComponentBuilder ("layout" in the code above).

In another class (called MainClass) I am using Lombok's @log4j2 annotation. In that file I am entering the following log entry:

  log.info("Application has started!");   

When running my application, I would expect the log output to be:

2016-06-20 19:23:48,202 [INFO ] net.factor3.apps.MainClass - Application has started!

Instead, I am seeing:

Application has started!

I do not understand why the pattern I am setting is not being displayed.

Have I found a bug in Log4j2's pattrn API? Or am I missing something that is causing the logger to "ignore" my pattern settings? Someone please advise.

Factor Three
  • 2,094
  • 5
  • 35
  • 51
  • Does [Log4j2: The logs are not getting written to the file](https://stackoverflow.com/q/69878351/11748454) answer your question? – Piotr P. Karwasz Jul 25 '23 at 06:33
  • No, I do not believe that it does. That is a different problem, where logs are not being written to a file. This is a problem where logs are being written, but they are not being written in the format that I am trying to set. It is not a matter of getting no output, but one where the output is not as it is supposed to be,. – Factor Three Aug 02 '23 at 15:12
  • The problems are different, but I suspect that the cause is common: use `Configurator.reconfigure` unless you are sure nothing has initialized the logger context yet. – Piotr P. Karwasz Aug 02 '23 at 15:54

0 Answers0