log4j reset properties back to original as in log4j.properties file - java

In my application, I defined log4j.properites as follows
log4j.appender.email=org.apache.log4j.net.SMTPAppender
log4j.appender.email.Subject=email Notification
later in the program i am dynamically changing the subject to
Properties prop = new Properties();
prop.setProperty("log4j.appender.email.Subject", "Test Completed");
After I use this variable, I wan to reset this back to original on file. so I did this
LogManager.resetConfiguration();
PropertyConfigurator.configure(prop);
But, later in the code whenever I use this subject property it is giving its value as 'Test Completed'. Any suggestion to reset configuration is greatly appreciated. Thanks

As stated in my comments above both
LogManager.resetConfiguration();
PropertyConfigurator.configure(prop);
don't reconfigure already existing Logger instances, so that they still use your old EmailAppender. In order to make changes to take effect you should recreate loggers.
If it's not possible (your loggers are static final fields for example), you can create a simple Logger wrapper, which will register itself with some listener, that will notify on configuration change, so that wrapper can create fresh logger instance

This is a partial answer (I know); But you can do a reset without explicit LogManager.resetConfiguration();
Resetting Hierarchy
The hierarchy will be reset before configuration when log4j.reset=true is present in the properties file.
https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PropertyConfigurator.html

Related

In focframework were can I get a list of all properties supported in config.properties file, and how to add my own properties to be used in my own

I am developing a web application using the full stack framework focframework, and I want to know what are the properties that I can control in my config.properties file. Is there a doc for this?
I tried searching the doc but dodn't find anything
Obviously we can figure out some of then from the sample on GitHub by looking at the config.properties file:
jdbc.drivers=org.h2.Driver
jdbc.url=jdbc:h2:./myfocapplication_data_h2
jdbc.username=sa
jdbc.password=
gui.rtl=0
allowAddInsideComboBox=0
focWebServerClassName=com.focframework.sample.myfocapplication.MyFocAppWebServer
dataSourceClass=b01.focDataSourceDB.FocDataSource_DB
cloudStorageClass=com.focCloudStorage.FocCloudStorageS3
cloudStorageClass=com.foc.cloudStorage.FocCloudStorage_LocalDisc
devMode=1
unitDevMode=0
unitAllowed=1
log.dir=c:/01barmaja/log
log.ConsoleActive=1
log.fileActive=1
log.popupExceptionDialog=1
log.dbRequest=1
log.dbSelect=1
debug.showStatusColumn=0
log.debug=1
perf.active=0
Is there any hint on how to get all of them? And what if I want to add my own to be used in my code?
The ConfigInfo.java file is the one responsible of reading all the properties and storing them in variables. It is straight forward to understand and check the variables names and usage. Yet I agree that someone should work on the documentation and add these parameters.
To add your own without modifying the ConfigInfo.java you can simply use this method in the middle of your code.
String myProperty = ConfigInfo.getProperty("my.property.with.a.meanignful.name");

Java how to read parameters from both file and cli?

I'm writing a tool in java and I need to provide some parameters that user can set.
I thought it is good to have ability to save all parameters in a file (and just run the .jar) and to alter saved parameters through command line.
So, I need to somehow handle parameters from two sources (priority, validity, etc.). Currently I use Apache.commons.cli to read cli-provided parameters and java.util.Properties for file-provided properties. And then I combine these properties together (and add some defaults if needed). But I don't like the result, it seems over-complicated to me.
So the code is something like this:
Properties fromFile = new Properties();
fromFile.load(new FileInputStream("settings.properties"));
cli.Options cliOptions = new cli.Options();
cliOptions.addOption(longName, shortName, hasArg, description);
//add more options
Parser parser = new DefaultParser();
CommandLine fromCli = parser.parse(cliOptions, args);
//at this point I have two different objects with properties I need,
//and I need to get every property from fromCli, check it's not empty,
// if it is, get it from fromFile, etc
So the question is: is there any library to handle properties from different sources (cli, file, defaults)? I tried googling, but did not succeed. Sorry if my googling skills are just not enough.
I'd like the code to be something like this:
import org.supertools.allPropsLib;
allPropsLib.PropsHandler handler = new allPropsLib.PropsHandler();
handler.addOptions(name, shortName, hasArg, description, defaultsTo);
handler.addSource(allPropsLib.Sources.CLI);
handler.addSource(allPropsLib.Sources.FILE);
handler.addSource(allPropsLib.Sources.DEFAULTS);
handler.setFileSource("filename");
allPropsLib.PropsContainer properties = handler.readAllProps();
// and at this point container should contain properties combined
// maybe there should be some handler function to tell the priorities,
// but I don't need to decide from where each properties should be taken
After you define the properties, load them into a java.util.Properties container regardless of the source. Then call the logic and pass it the container as a parameter.

Liferay: Get URL in portal.normal.vm from properties file

I have problems to read info from properties file "liferay-portal-6.1.0/tomcat-7.0.23/lib/myweb-application.properties" in portal_normal.vm
myweb-application.properties:
redirect.docs.url = http://stackoverflow.com/questions/ask
"liferay-portal-6.1.0/tomcat-7.0.23/webapps/web-theme/templates/portal_normal.vm":
#set ($docsURL = $propsUtil.get("redirect.docs.url")) #language("foot.docs")
Get value of portal-ext.properties
eg. test.name="sachin"
${propsUtil.get("test.name")}
PropsUtil (or $propsUtil) accesses portal.properties, typically configured through portal-ext.properties. So unless you add your myweb-application.properties as an "external properties" file, PropsUtil won't find it.
One way to do this is to add this line to your portal-ext.properties:
include-and-override=/path/to/myweb-application.properties
but make sure it's not using the same keys as portal.properties for different purposes.

java.util.log , using 3 different logger instances/log files, getting .1 .2 etc

I'm trying to figure out why I'm generating multiple versions of my log files (.1, .2 ,etc). My project has a need for 3 separate loggers, I'm using java.util.log and setting up my loggers as follows:
logger = Logger.getLogger("Logger");
logger.addHandler(new FileHandler(logFileName));
statsLogger = Logger.getLogger("StatsLogger");
statsLogger.addHandler(new FileHandler(statsLogFileName));
alarmLogger = Logger.getLogger("AlarmLogger");
alarmLogger.addHandler(new FileHandler(alarmLogFileName));
my logging properties file:
handlers=java.util.logging.ConsoleHandler java.util.logging.FileHandler
java.util.logging.FileHandler.append=true
java.util.logging.FileHandler.level=INFO
java.util.logging.ConsoleHandler.level=INFO
java.util.logging.ConsoleHandler.formatter=com.package.LogFormatter
java.util.logging.FileHandler.formatter=com.package.LogFormatter
This seems to work, well as far as I can tell anyway... things seem to be getting logged to the correct log files. Any ideas on what could be causing this?
Thanks for any ideas
According to the FileHandler javadoc
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/logging/FileHandler.html#FileHandler
Normally the "%u" unique field is set to 0. However, if the
FileHandler tries to open the filename and finds the file is currently
in use by another process it will increment the unique number field
and try again. This will be repeated until FileHandler finds a file
name that is not currently in use. If there is a conflict and no "%u"
field has been specified, it will be added at the end of the filename
after a dot. (This will be after any automatically added generation
number.)
Have you got multiple processes running that are using the same log configuration ? Also check that any old test processes are not showing up in windows process viewer or top.

Setting jvmargs in log4j.properties file

This is a bit of a weird request but I am trying to set some jvmargs in the log4j.properties file. At present I use ant to for example set some args....
jvmarg value="-Dmail.smtp.socketFactory.port=465"
... but it would be great to group a few of these logging relevant arguments into the .properties file. Does anyone know how to do this?
Thanks in advance!
Log4j is only going to read the properties file after the JVM has already started - that means it can't affect the JVM arguments.
If the properties can be added after JVM startup, you could add a property to your properties file that lists all properties that you want to add to the SystemProperties collection, something like:
# property names of system properties
systemprops=mail.smtp.port mail.smtp.socketFactory.class
mail.smtp.port=465
mail.smtp.socketFactory.class=some.class
Your startup code can read the systemprops value, split on whitespace and add the resulting list of properties to the SystemProperties collection while reading the values from your properties collection.
This way your code does not need to know which properties to add to system props, only that the properties to add are defined by the systemprops property.
If your example is from your actual situation then you can set this value programatically for the java mail...
The SMTP protocol provider supports
the following properties, which may be
set in the JavaMail Session object.
The properties are always set as
strings; the Type column describes how
the string is interpreted. For
example, use
props.put("mail.smtp.port", "888");
http://java.sun.com/products/javamail/javadocs/com/sun/mail/smtp/package-summary.html
This example above should work for mail.smtp.socketFactory.port also.

Categories

Resources