Yes.  Approaching from use case may be better.

I am trying to port some software from lo4j1. This is the first time I have
used log4j, and don't have access to the original authors.
In that code, they extended Logger classes in ways that are no longer
supported in log4j2.
They created custom loggers for individual objects, each of these loggers
had a bit of custom message formatting code and would write to their own
log file with a unique name.
 The loggers were stored in a hashmap using a key from the object they were
associated with.  As the objects did things, they grabbed their logger from
the hashmap and used it.




On Fri, May 20, 2022 at 8:16 AM Volkan Yazıcı <vol...@yazi.ci> wrote:

> Stephen, given your past questions, I see that you are trying to
> programmatically configure Log4j with interesting features, e.g., multiple
> logger contexts and such. May I ask what is your use case? What are you
> exactly trying to solve?
>
> On Thu, May 19, 2022 at 5:57 PM Stephen Johns <mr.stephen.jo...@gmail.com>
> wrote:
>
> > I am trying to create a completely separate second
> > Context/Configuration/Logger - not a logger within an existing
> > config/context.
> > Log messages are going to STDOUT.
> >
> >
> > Current code
> >
> >         ConfigurationBuilder<BuiltConfiguration> _configurationBuilder =
> > ConfigurationBuilderFactory.newConfigurationBuilder();
> >         _configurationBuilder.setConfigurationName("SMDR_DEBUG_" +
> pName);
> >         LoggerContext _loggerContext = new LoggerContext("SMDR_DEBUG_" +
> > pName);
> >         _configurationBuilder.setLoggerContext(_loggerContext);
> >         _configurationBuilder.setStatusLevel(Level.TRACE);
> >
> >
> >         // Create the appender
> >         AppenderComponentBuilder log4jFileAppenderBuilder =
> > _configurationBuilder.
> >                 newAppender(pName + "_SmdrDailyRollingFileAppender",
> > "RollingFile");
> >         log4jFileAppenderBuilder.addAttribute("filename", pLogFilename);
> >         log4jFileAppenderBuilder.addAttribute("filePattern",
> > pLogFilenamePattern);
> >
> >         // Setup roll-over
> >         ComponentBuilder triggeringPolicy =
> > _configurationBuilder.newComponent("Policies")
> >
> >
> >
> .addComponent(_configurationBuilder.newComponent("TimeBasedTriggeringPolicy").
> >                         addAttribute("interval", "1"));
> >         log4jFileAppenderBuilder.addComponent(triggeringPolicy);
> >
> >         // Configure the PatternLayout
> >         LayoutComponentBuilder layoutComponentBuilder =
> > _configurationBuilder.newLayout("PatternLayout").
> >                 addAttribute("pattern", DEBUG_PATTERN_LAYOUT_STRING);
> >         log4jFileAppenderBuilder.add(layoutComponentBuilder);
> >
> >         // Add it back into configuration
> >         _configurationBuilder.add(log4jFileAppenderBuilder);
> >
> >         // https://logging.apache.org/log4j/2.x/manual/customconfig.html
> >         RootLoggerComponentBuilder loggerBuilder =
> > _configurationBuilder.newRootLogger(Level.DEBUG);
> >         loggerBuilder.add(_configurationBuilder.newAppenderRef(pName +
> > "_SmdrDailyRollingFileAppender"));
> >         loggerBuilder.addAttribute("additivity", false);
> >         _configurationBuilder.add(loggerBuilder);
> >
> >         LoggerContext _lc =
> > Configurator.initialize(_configurationBuilder.build());
> >
> >         System.out.println("***** SRJ SRJ SMDR context from initialize
> is "
> > + _lc);
> >
> >         Logger _g = _loggerContext.getRootLogger();
> >         System.out.println("***** SRJ SRJ SMDR rootlogger from context
> is "
> > + _g);
> >         _g.error("***** SRJ SRJ ROOT LOGGER IN SMDR_DEBUG.txt");
> >
> >         Logger _gg = _loggerContext.getLogger(pName);
> >         System.out.println("***** SRJ SRJ SMDR logger "+pName+" from
> > context is " + _gg);
> >         _gg.error("***** SRJ SRJ "+pName+" LOGGER IN SMDR_DEBUG.txt");
> >
> > The .error() calls above go to STDOUT.  Note that I have tried using
> > reconfigure instead of initialize, but that messes up my original
> > configuration.
> >
> > The loggers seem wrong, as I print them out and they seem like they are
> > right name, but at error level
> > ***** SRJ SRJ SMDR rootlogger from context is :ERROR in SMDR_DEBUG_Global
> > 16:23:59.989 [main] ERROR  - ***** SRJ SRJ ROOT LOGGER IN SMDR_DEBUG.txt
> > <-- should be in log file
> > ***** SRJ SRJ SMDR logger Global from context is Global:ERROR in
> > SMDR_DEBUG_Global
> > 16:23:59.990 [main] ERROR Global - ***** SRJ SRJ Global LOGGER IN
> > SMDR_DEBUG.txt   <-- should be in log file
> >
> >
> > XML generated from builder:
> > <?xml version="1.0" ?>
> > <Configuration name="SMDR_DEBUG_Global" status="TRACE">
> >     <Appenders>
> >         <RollingFile name="Global_SmdrDailyRollingFileAppender"
> > filename="ps/debug/SMDR_DEBUG.txt"
> >
> filePattern="ps/debug/SMDR_DEBUG_%d{yyyyMMdd}.txt.gz">
> >             <Policies>
> >                 <TimeBasedTriggeringPolicy interval="1"/>
> >             </Policies>
> >             <PatternLayout pattern="%d{MM.DD.yy-HH:mm:ss} %m%n"/>
> >         </RollingFile>
> >     </Appenders>
> >     <Loggers>
> >         <Root level="DEBUG" additivity="false">
> >             <AppenderRef ref="Global_SmdrDailyRollingFileAppender"/>
> >         </Root>
> >     </Loggers>
> > </Configuration>
> >
> >
> > Trace from builder:
> >
> > 2022-05-19 16:23:59,921 main DEBUG PluginManager 'Converter' found 45
> > plugins
> > 2022-05-19 16:23:59,922 main DEBUG Starting OutputStreamManager
> > SYSTEM_OUT.false.false-3
> > 2022-05-19 16:23:59,940 main INFO Log4j appears to be running in a
> Servlet
> > environment, but there's no log4j-web module available. If you want
> better
> > web container support, please add the log4j-web JAR to your web archive
> or
> > server lib directory.
> > 2022-05-19 16:23:59,941 main DEBUG Apache Log4j Core 2.17.1 initializing
> > configuration
> >
> >
> org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration@3a3e78f
> > 2022-05-19 16:23:59,942 main DEBUG Installed 1 script engine
> > 2022-05-19 16:23:59,963 Thread Context Data Task DEBUG Initializing
> Thread
> > Context Data Service Providers
> > 2022-05-19 16:23:59,964 Thread Context Data Task DEBUG Thread Context
> Data
> > Service Provider initialization complete
> > 2022-05-19 16:23:59,969 main DEBUG Oracle Nashorn version: 1.8.0_252,
> > language: ECMAScript, threading: Not Thread Safe, compile: true, names:
> > [nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScript,
> ecmascript],
> > factory class: jdk.nashorn.api.scripting.NashornScriptEngineFactory
> > 2022-05-19 16:23:59,969 main DEBUG PluginManager 'Core' found 127 plugins
> > 2022-05-19 16:23:59,969 main DEBUG PluginManager 'Level' found 0 plugins
> > 2022-05-19 16:23:59,970 main DEBUG PluginManager 'Lookup' found 16
> plugins
> > 2022-05-19 16:23:59,970 main DEBUG Building Plugin[name=AppenderRef,
> > class=org.apache.logging.log4j.core.config.AppenderRef].
> > 2022-05-19 16:23:59,971 main DEBUG
> > createAppenderRef(ref="Global_SmdrDailyRollingFileAppender",
> level="null",
> > Filter=null)
> > 2022-05-19 16:23:59,971 main DEBUG Building Plugin[name=root,
> > class=org.apache.logging.log4j.core.config.LoggerConfig$RootLogger].
> > 2022-05-19 16:23:59,972 main DEBUG createLogger(additivity="false",
> > level="DEBUG", includeLocation="null",
> > ={Global_SmdrDailyRollingFileAppender}, ={},
> > Configuration(SMDR_DEBUG_Global), Filter=null)
> > 2022-05-19 16:23:59,972 main DEBUG Building Plugin[name=loggers,
> > class=org.apache.logging.log4j.core.config.LoggersPlugin].
> > 2022-05-19 16:23:59,973 main DEBUG createLoggers(={root})
> > 2022-05-19 16:23:59,973 main DEBUG Building
> > Plugin[name=TimeBasedTriggeringPolicy,
> >
> >
> class=org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy].
> > 2022-05-19 16:23:59,975 main DEBUG
> > TimeBasedTriggeringPolicy$Builder(interval="1", modulate="null",
> > maxRandomDelay="null")
> > 2022-05-19 16:23:59,975 main DEBUG Building Plugin[name=Policies,
> >
> >
> class=org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy].
> > 2022-05-19 16:23:59,975 main DEBUG
> > createPolicy(={TimeBasedTriggeringPolicy(nextRolloverMillis=0,
> interval=1,
> > modulate=false)})
> > 2022-05-19 16:23:59,975 main DEBUG Building Plugin[name=layout,
> > class=org.apache.logging.log4j.core.layout.PatternLayout].
> > 2022-05-19 16:23:59,976 main DEBUG
> > PatternLayout$Builder(pattern="%d{MM.DD.yy-HH:mm:ss} %m%n",
> > PatternSelector=null, Configuration(SMDR_DEBUG_Global), Replace=null,
> > charset="null", alwaysWriteExceptions="null", disableAnsi="null",
> > noConsoleNoAnsi="null", header="null", footer="null")
> > 2022-05-19 16:23:59,976 main DEBUG PluginManager 'Converter' found 45
> > plugins
> > 2022-05-19 16:23:59,982 main DEBUG Building Plugin[name=appender,
> > class=org.apache.logging.log4j.core.appender.RollingFileAppender].
> > 2022-05-19 16:23:59,983 main DEBUG
> > RollingFileAppender$Builder(fileName="ps/debug/SMDR_DEBUG.txt",
> > filePattern="ps/debug/SMDR_DEBUG_%d{yyyyMMdd}.txt.gz", append="null",
> > locking="null",
> >
> >
> Policies(CompositeTriggeringPolicy(policies=[TimeBasedTriggeringPolicy(nextRolloverMillis=0,
> > interval=1, modulate=false)])), Strategy=null, advertise="null",
> > advertiseUri="null", createOnDemand="null", filePermissions="null",
> > fileOwner="null", fileGroup="null", bufferedIo="null", bufferSize="null",
> > immediateFlush="null", ignoreExceptions="null",
> > PatternLayout(%d{MM.DD.yy-HH:mm:ss} %m%n),
> > name="Global_SmdrDailyRollingFileAppender",
> > Configuration(SMDR_DEBUG_Global), Filter=null, ={})
> > 2022-05-19 16:23:59,984 main TRACE New file 'ps/debug/SMDR_DEBUG.txt'
> > created = true
> > 2022-05-19 16:23:59,984 main DEBUG Returning file creation time for
> > /opt/SecureLogix/ETM/ps/debug/SMDR_DEBUG.txt
> > 2022-05-19 16:23:59,984 main DEBUG Starting RollingFileManager
> > ps/debug/SMDR_DEBUG.txt
> > 2022-05-19 16:23:59,985 main DEBUG PluginManager 'FileConverter' found 2
> > plugins
> > 2022-05-19 16:23:59,985 main DEBUG Setting prev file time to
> > 2022-05-19T16:23:59.000+0100
> > 2022-05-19 16:23:59,985 main DEBUG Initializing triggering policy
> >
> >
> CompositeTriggeringPolicy(policies=[TimeBasedTriggeringPolicy(nextRolloverMillis=0,
> > interval=1, modulate=false)])
> > 2022-05-19 16:23:59,986 main DEBUG Initializing triggering policy
> > TimeBasedTriggeringPolicy(nextRolloverMillis=0, interval=1,
> modulate=false)
> > 2022-05-19 16:23:59,987 main TRACE PatternProcessor.getNextTime returning
> > 2022/05/20-00:00:00.000, nextFileTime=2022/05/19-00:00:00.000,
> > prevFileTime=1970/01/01-01:00:00.000, current=2022/05/19-16:23:59.986,
> > freq=DAILY
> > 2022-05-19 16:23:59,988 main TRACE PatternProcessor.getNextTime returning
> > 2022/05/20-00:00:00.000, nextFileTime=2022/05/19-00:00:00.000,
> > prevFileTime=2022/05/19-00:00:00.000, current=2022/05/19-16:23:59.988,
> > freq=DAILY
> > 2022-05-19 16:23:59,988 main DEBUG Building Plugin[name=appenders,
> > class=org.apache.logging.log4j.core.config.AppendersPlugin].
> > 2022-05-19 16:23:59,988 main DEBUG
> > createAppenders(={Global_SmdrDailyRollingFileAppender})
> > 2022-05-19 16:23:59,989 main DEBUG Configuration
> >
> >
> org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration@3a3e78f
> > initialized
> >
> > --
> >    ☮
> >
>


-- 
   ☮

Reply via email to