Setting jvmargs in log4j.properties file - java

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.

Related

application.yaml file setting field with OR condition (Providing default value)

There is a Java SpringBoot application, with an application.yaml file where all the properties are configured. some of the fields are configured to read from the deployment.yaml file st eince we use different environments, and the values for these fields changes. However I wan't to define a field which will consider the value from deployment.yaml file if it is present, else it should take the default value given.
Something like this:
root:
some-sub-level:
some-key: ${VALUE_FROM_DEPLOYMENT_YAML:${default.level.value}}
default:
level:
value: some-default-value
I tried several ways and couldn't succeed. It takes blank value since VALUE_FROM_DEPLOYMENT_YAML will be null in one particular environment.
PS: I'm aware of adding default value in Java code like with #Value("${value.from.application.yaml:"some default value"}, but I'm not interested in this. The requirement is not to read this in Java code but it will be a configuration for Azure Application Insight.
Could anyone help me what I'm missing here
According to this answer, it supports this notation which is called Placeholder property:
spring:
profiles:
active: ${APP_PROFILE:test}
# ... #

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");

How to obtain Felix Framework's properties in the config.properties file?

I have this line of code that returns me null and it should return 9193 which is the value that the property really has in my config.properties file...
System.getProperty("org.osgi.service.http.port") // returns null dunno why
My config.properties has the property correctly set up:
org.osgi.service.http.port=9193
Probably I am missing something here, because it is the first time I try to access those properties. I've googled a lot, so there is lack of examples online or I didn't search for the correct keywords.
What should I add/change to that line to make it return the correct value?
Thanks!
Try to use BundleContext.getProperty. The config.properties are set as OSGi framework properties. So this should be a way to retrieve them.

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.

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

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

Categories

Resources