how to reinitialize log4j2 configuration? - configuration

After an initial configuration of log4j2 with:
Configurator.initialize(null, configLocation);
I would like to reinitialize it with a different URL
Configurator.initialize(null, configLocation2);
The problem is that the second call is ignored. I believe that once the LoggerContext is STARTED it will ignore reconfigurations.
Is there a way to do this?

Once you have a LoggerContext you can call
context.setConfigLocation(configLocation)
where configLocation is a URI. That will force a reconfiguration.

Related

MUnit test fails - Cannot process event as “FileConnector” is stopped

I am implementing Munit for a flow which involves Mule Requester. This mule requester would be picking up a file.
So, when i run the java class as Junit, it throws out an exception as, Cannot perform the operation on the FileConnector as it is stopped.
The expression used in mule requester is ,
file ://${path}?connector=FileConnector
I have also defined a global file connector.
Please let me know how to resolve this issue.
Thank you.
All connectors and inbound-endpoints are disabled by default in MUnit. This is to prevent flow accidentally processing/generating real data. (Some explanation here). For the same reason File Connector is also disabled.
To enable connectors, you need to override a method in your MUnitsuite as below -
#Override
protected boolean haveToMockMuleConnectors() {
return false;
}
For XML Munit, see this to enable connectors.
Note: This will enable and start all the connectors that you are using in your mule-configs under test. If you have SMTP connector, DB connector, MQ connector etc, they all be started during test, so use it with caution.
Check whether the file connector is defined in the files you loaded for munit.
<spring:beans>
<spring:import resource="classpath:api.xml"/>
</spring:beans>
You may also try mocking the mule requester.

is logback losing messages upon configuration reload?

I am trying to figure out whether logback is potentially losing messages.
Quoting from the log4j2 page:
"Like Logback, Log4j 2 can automatically reload its configuration upon modification. Unlike Logback, it will do so without losing log events while reconfiguration is taking place"
So, can anyone comment on logback losing log events? Does it really happen?
(I have seen that event loss might occur using Async appenders, but can be solved using setting discardingThreshold 0, but the statement on log4j2 is talking about configuration reload)
I am trying to understand whether log4j2 is really more reliable, or shall we just use logback...
Thanks.
When logback is reconfigured it removes all the appender references and level settings from the loggers. It then reads the new configuration and applies them to the loggers. While this is happening logging is still continuing.
Log4j 2 separates the loggers from their configuration. Once a new configuration is created the loggers are pointed at the LoggerConfig of the new configuration. So for a brief time you will have some loggers pointing at the old configuration and some pointing at the new one, but they will never be unconfigured.
I'm currently investigating this very question and, looking at the code, it indeed seems that log messages can be lost during reconfiguration. The following code is called by ReconfigureOnChangeFilter's ReconfiguringThread:
private void performXMLConfiguration(LoggerContext lc) {
JoranConfigurator jc = new JoranConfigurator();
jc.setContext(context);
StatusUtil statusUtil = new StatusUtil(context);
List<SaxEvent> eventList = jc.recallSafeConfiguration();
URL mainURL = ConfigurationWatchListUtil.getMainWatchURL(context);
lc.reset();
long threshold = System.currentTimeMillis();
try {
jc.doConfigure(mainConfigurationURL);
if (statusUtil.hasXMLParsingErrors(threshold)) {
fallbackConfiguration(lc, eventList, mainURL);
}
} catch (JoranException e) {
fallbackConfiguration(lc, eventList, mainURL);
}
}
In lc.reset(), all appenders are removed (along with other configuration attributes), then the logger context is reconfigured. There is no apparent synchronization going on.
A quick test verified that messages are lost during reconfiguration.

log4j not working in the first time

I am tring to implement log4j for the first time. My log4j.properties file is as given below.
# Define the root logger with appender file
log = /log/log4j
log4j.rootLogger = debug, NewStudentListDAO
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=/log/log4j/log.out
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
I have placed this file under classes folder and src folder. Still it does not give out to file. The log4j is invoked in my class NewStudentListDAO in the following lines. The site is not allowing me to submit this much alone so I am adding some special characters to fill the gap. Kindly ignore the same...
static Logger logger = Logger.getLogger( NewStudentListDAO.class.getName());
logger.setLevel(Level.ALL);
logger.debug("This is debug message from logger");
logger.info("This is info message from logger");
logger.log(Level.DEBUG, "This is a debug message from logger");
Kindly help me in fixing the issue.
Thanks
Saji
Try this adding to your logger file, and check whether it is first displaying on your console.
log4j.rootLogger=INFO, CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
Look at this link
Log4j: How to configure simplest possible file logging?.
I hope it helps.

Do I need call monkey.patch_all() in Django+Gunicorn+GEvent+Boto structure?

My website is using Django+Gunicorn+GEvent.
There is a function which I have to use Boto for DynamoDB.
Do I need call monkey.patch_all() to make Boto become greenlet?
If you use the default worker_class configuration, then you don't have the features of gevent. Look the doc here. I think you don't have the advantage of using gevent when you use the default configuration even though you monkey patch all.
So you should configure gunicorn to use the GeventWorker which does the monkey.patch_all() operation and in this situation, I think you don't have to monkey patch all. Here is the source code of GeventWorker and the doc about worker_class

setting hadoop job configuration programmatically

I am getting OOM exception (Java heap space) for reduce child. I read in the documentation that increasing the value of mapred.reduce.child.java.opts to -Xmx512M or more would help. Since I am not the admin, I cannot change that value in mapred-site.xml. I would like to set that value only for my job through the java program. I tried setting it using Configuration class as follows, but that didn't work.
Configuration config = new Configuration();
config.set("mapred.reduce.child.java.opts", "-Xmx512M");
JobConf conf1 = new JobConf(config, this.getClass());
The version of Hadoop is 1.0.3
What is the proper way of setting the configuration values programmatically?
AS #ThomasJungblut and #octo have pointed out, the procedure I mentioned in the question is the right way of doing it. The OOM exception still persists, so I would start a new thread instead of continuing here.