Re: Log4j2 replacement of PropertyConfigurator APIs configureAndWatch and configure
First, I strongly recommend you switch from properties to either XML, Yaml, or JSON. To prgormatically configure Log4j2 do Configurator.initialize(“ConfigName”, “log4j2.xml”); Note that there are several variations of the initialize method. If the configuration contains a monitorInterval than Log4j will automatically watch for changes. Ralph > On Mar 17, 2023, at 8:59 PM, Viraj Jasani wrote: > > Hi, > > Could you please help with log4j2 replacement for PropertyConfigurator APIs > configureAndWatch and configure? > > For instance, this is the logic we have that we need to migrate to log4j2: > > > > protected void initLog() throws ServerException { >verifyDir(logDir); >LogManager.resetConfiguration(); >File log4jFile = new File(configDir, name + "-log4j.properties"); >if (log4jFile.exists()) { > PropertyConfigurator.configureAndWatch(log4jFile.toString(), 10 * > 1000); //every 10 secs > log = LoggerFactory.getLogger(Server.class); >} else { > Properties props = new Properties(); > try { >InputStream is = getResource(DEFAULT_LOG4J_PROPERTIES); >try { > props.load(is); >} finally { > is.close(); >} > } catch (IOException ex) { >throw new ServerException(ServerException.ERROR.S03, > DEFAULT_LOG4J_PROPERTIES, ex.getMessage(), ex); > } > PropertyConfigurator.configure(props); > log = LoggerFactory.getLogger(Server.class); > log.warn("Log4j [{}] configuration file not found, using default > configuration from classpath", log4jFile); >} > }
Re: Log4j2 replacement of PropertyConfigurator APIs configureAndWatch and configure
Thanks Ralph for the recommendations! > First, I strongly recommend you switch from properties to either XML, Yaml, or JSON. The reason why we would still like to stick to properties (at least for now during the migration) is that LOG4J2-3341 allows setting level and appender at once in the properties file. After migration, if there is any better way, we would consider migrating to xml or yaml for sure. Here is the old thread for the reference[1] after which both hbase and hadoop have decided to stick to properties for now. > Note that there are several variations of the initialize method. Sure I think we might be able to use, will explore: public static LoggerContext initialize(final String name, final ClassLoader loader, final URI configLocation) { return initialize(name, loader, configLocation, null); } Even with properties file, is it still possible to configure monitor interval? 1. https://lists.apache.org/thread/gvfb3jkg6t11cyds4jmpo7lrswmx28w3 On Fri, Mar 17, 2023 at 8:59 PM Viraj Jasani wrote: > Hi, > > Could you please help with log4j2 replacement for PropertyConfigurator > APIs configureAndWatch and configure? > > For instance, this is the logic we have that we need to migrate to log4j2: > > > > protected void initLog() throws ServerException { > verifyDir(logDir); > LogManager.resetConfiguration(); > File log4jFile = new File(configDir, name + "-log4j.properties"); > if (log4jFile.exists()) { > PropertyConfigurator.configureAndWatch(log4jFile.toString(), 10 * > 1000); //every 10 secs > log = LoggerFactory.getLogger(Server.class); > } else { > Properties props = new Properties(); > try { > InputStream is = getResource(DEFAULT_LOG4J_PROPERTIES); > try { > props.load(is); > } finally { > is.close(); > } > } catch (IOException ex) { > throw new ServerException(ServerException.ERROR.S03, > DEFAULT_LOG4J_PROPERTIES, ex.getMessage(), ex); > } > PropertyConfigurator.configure(props); > log = LoggerFactory.getLogger(Server.class); > log.warn("Log4j [{}] configuration file not found, using default > configuration from classpath", log4jFile); > } > } > > >
Re: Log4j2 replacement of PropertyConfigurator APIs configureAndWatch and configure
Ah looks like monitorinterval is supported as per the doc[1]: Properties configuration files support the advertiser, monitorInterval, name, packages, shutdownHook, shutdownTimeout, status, verbose, and dest attributes. 1. https://logging.apache.org/log4j/2.x/manual/configuration.html#ConfigurationSyntax On Sat, Mar 18, 2023 at 11:14 AM Viraj Jasani wrote: > Thanks Ralph for the recommendations! > > > First, I strongly recommend you switch from properties to either XML, > Yaml, or JSON. > > The reason why we would still like to stick to properties (at least for > now during the migration) is that LOG4J2-3341 allows setting level and > appender at once in the properties file. After migration, if there is any > better way, we would consider migrating to xml or yaml for sure. Here is > the old thread for the reference[1] after which both hbase and hadoop have > decided to stick to properties for now. > > > Note that there are several variations of the initialize method. > > Sure I think we might be able to use, will explore: > public static LoggerContext initialize(final String name, final > ClassLoader loader, final URI configLocation) { > return initialize(name, loader, configLocation, null); > } > > Even with properties file, is it still possible to configure monitor > interval? > > > 1. https://lists.apache.org/thread/gvfb3jkg6t11cyds4jmpo7lrswmx28w3 > > On Fri, Mar 17, 2023 at 8:59 PM Viraj Jasani wrote: > >> Hi, >> >> Could you please help with log4j2 replacement for PropertyConfigurator >> APIs configureAndWatch and configure? >> >> For instance, this is the logic we have that we need to migrate to log4j2: >> >> >> >> protected void initLog() throws ServerException { >> verifyDir(logDir); >> LogManager.resetConfiguration(); >> File log4jFile = new File(configDir, name + "-log4j.properties"); >> if (log4jFile.exists()) { >> PropertyConfigurator.configureAndWatch(log4jFile.toString(), 10 * >> 1000); //every 10 secs >> log = LoggerFactory.getLogger(Server.class); >> } else { >> Properties props = new Properties(); >> try { >> InputStream is = getResource(DEFAULT_LOG4J_PROPERTIES); >> try { >> props.load(is); >> } finally { >> is.close(); >> } >> } catch (IOException ex) { >> throw new ServerException(ServerException.ERROR.S03, >> DEFAULT_LOG4J_PROPERTIES, ex.getMessage(), ex); >> } >> PropertyConfigurator.configure(props); >> log = LoggerFactory.getLogger(Server.class); >> log.warn("Log4j [{}] configuration file not found, using default >> configuration from classpath", log4jFile); >> } >> } >> >> >>
Re: Log4j2 replacement of PropertyConfigurator APIs configureAndWatch and configure
Yes, everything is supported in the Properties format, but the syntax is confusing to almost everyone. Ralph > On Mar 18, 2023, at 11:27 AM, Viraj Jasani wrote: > > Ah looks like monitorinterval is supported as per the doc[1]: > Properties configuration files support the advertiser, monitorInterval, > name, packages, shutdownHook, shutdownTimeout, status, verbose, and dest > attributes. > > 1. > https://logging.apache.org/log4j/2.x/manual/configuration.html#ConfigurationSyntax > > On Sat, Mar 18, 2023 at 11:14 AM Viraj Jasani wrote: > >> Thanks Ralph for the recommendations! >> >>> First, I strongly recommend you switch from properties to either XML, >> Yaml, or JSON. >> >> The reason why we would still like to stick to properties (at least for >> now during the migration) is that LOG4J2-3341 allows setting level and >> appender at once in the properties file. After migration, if there is any >> better way, we would consider migrating to xml or yaml for sure. Here is >> the old thread for the reference[1] after which both hbase and hadoop have >> decided to stick to properties for now. >> >>> Note that there are several variations of the initialize method. >> >> Sure I think we might be able to use, will explore: >>public static LoggerContext initialize(final String name, final >> ClassLoader loader, final URI configLocation) { >>return initialize(name, loader, configLocation, null); >>} >> >> Even with properties file, is it still possible to configure monitor >> interval? >> >> >> 1. https://lists.apache.org/thread/gvfb3jkg6t11cyds4jmpo7lrswmx28w3 >> >> On Fri, Mar 17, 2023 at 8:59 PM Viraj Jasani wrote: >> >>> Hi, >>> >>> Could you please help with log4j2 replacement for PropertyConfigurator >>> APIs configureAndWatch and configure? >>> >>> For instance, this is the logic we have that we need to migrate to log4j2: >>> >>> >>> >>> protected void initLog() throws ServerException { >>>verifyDir(logDir); >>>LogManager.resetConfiguration(); >>>File log4jFile = new File(configDir, name + "-log4j.properties"); >>>if (log4jFile.exists()) { >>> PropertyConfigurator.configureAndWatch(log4jFile.toString(), 10 * >>> 1000); //every 10 secs >>> log = LoggerFactory.getLogger(Server.class); >>>} else { >>> Properties props = new Properties(); >>> try { >>>InputStream is = getResource(DEFAULT_LOG4J_PROPERTIES); >>>try { >>> props.load(is); >>>} finally { >>> is.close(); >>>} >>> } catch (IOException ex) { >>>throw new ServerException(ServerException.ERROR.S03, >>> DEFAULT_LOG4J_PROPERTIES, ex.getMessage(), ex); >>> } >>> PropertyConfigurator.configure(props); >>> log = LoggerFactory.getLogger(Server.class); >>> log.warn("Log4j [{}] configuration file not found, using default >>> configuration from classpath", log4jFile); >>>} >>> } >>> >>> >>>