Logback: out-of-the-box properties - java

Where should I look to see the default logback properties, e.g. user.home?
It is great that I can use external file, System and environment to load properties, but are there any out-of-box ones?

user.home is a Java system property, not a logback property. You can see the rest of them with System.out.println(System.getProperties()); or using JConsole on a running JVM.
Apart from that, I think there are only HOSTNAME and CONTEXT_NAME defined by Logback: http://logback.qos.ch/manual/configuration.html#variableSubstitution

user.home is not logback specific, they come from java.
See http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html

Related

How to set contextName of log4j2 Mbean

While accessing log4j2 Mbeans using jvisualvm, I see that the type (contextName) inside log4j2 package is a number (for me it is 1482868390). Since this goes in as 'type' while specifying Object in query, I would like to specify it to a more recognizable name that I prefer. Is there a way to set the contextName in xml configuration.
I think there is a way to do it for web applications, but I would like to set this for a standalone java application.
Yes this is possible but not in configuration. You can achieve this by subclassing one of Log4j's ContextSelectors and overriding its defaultContextName() method.
You then tell Log4j to use your ContextSelector by starting your application with system property -DLog4jContextSelector=com.yourpackage.YourContextSelector.
If you're making all loggers async, you want to subclass AsyncLoggerContextSelector, otherwise ClassLoaderContextSelector.

How is it possible to reference a variable from log4j.properties file?

I need to solve the following task.
We are usign log4j in version 1.2x and want to keep it so if possible. We have a graylog appender in the log4j.properties file where we set the ip address and the port. If they are hardcoded, the logging to the gray log works.
The task is however to make these two parameters (IP and port) configurable somehow in the log4j.properties file.
Afterwards the operation team on each deployment environment would just set this configuration for the IP and port once and it would work in all the java projects that use this variable in log4j.properties file.
Ideal solution would be using environment variable of the operating system, but any other configuration as a property file (not that one of the log4j itself and except for using JVM arguments - this is not an option for us) or some other solution
I have read that this is possible with log4j version 2.x but this would take us quite a lot of time (converting the log4j.propeties files to log4j2.xml by hand, changing the code ..).
I`ll be glad for any reasonable answer.
This could be done in the log4j.properties file. Note, you can not use environment variables (that's a platform specific concept).
These 2 properties (IP and port) can be set in code or using the "-D" JVM option.
In your example:
java ... -Dip=%YOUR_IP% -Dport=%YOUR_PORT% ... your_app
This allows you to reference the property using ${...} notation
Eg:
log4j.graylog_appender.LOGGER.IP=${ip}
log4j.graylog_appender.LOGGER.PORT=${port}
Hope it helps!
System.setProperty("key", "value");

How to override log4j properties for all the imported Java libraries?

I am using Clojure but I think this is irrelevant to the question. I would like to override the log4j properties for all the imported libraries and have the exact same format for all. Right now it looks like this:
2014-11-26 19:37:19.399 INFO net.spy.memcached.auth.AuthThread: Authenticated to ae-couchbase10/10.52.61.37:11210
INFO: {:thread-name async-dispatch-2, :first_id batch::test::dev::934ebce6-b78d-4f7c-b297-f636cbfeca0c::8307a507-7deb-40dc-811a-b339148472e7, :time 171.587344, :perf 5.967806110455326}
What is the best way of doing that?
If you use XML-based configuration, ie the log4j.xml file, this will have a higher priority over any log4j.properties file that exists in any library.
But if you require the properties file, add an argument to the Java Virtual Machine, e.g.:
java -Dlog4j.configuration=file:/tmp/log4j.properties KillerApp
Agree witht he use of log4j.xml.
The cloure Luminus framework has a nice default setup for using log4j. Strongly encourage laziness and recommend ahving a look at that for one way of handling log4j setup in clojure projects.

Where is ${SHDP_AMSERVICE_PORT} for DefaultMindAppmasterServiceClient defined?

In the YARN Documentation, Section 11.12.2 Partitioning on Configuring Container the
DefaultMindAppmasterServiceClient is setup by the following:
<yarn-int:amservice-client
service-impl="org.springframework.yarn.integration.ip.mind.DefaultMindAppmasterServiceClient"
host="${SHDP_AMSERVICE_HOST}"
port="${SHDP_AMSERVICE_PORT}" />
How is the port SHDP_AMSERVICE_PORT defined for the amservice?
Your link includes this,
Through Spring's property placeholder support, SpEL and the environment abstraction (available in Spring 3.1). one can externalize environment specific properties from the main code base easing the deployment across multiple machines.
It also mentions support for Properties files. But it appears that it supports environment variables. On *nix type systems you might define it like
export SHDP_AMSERVICE_PORT=1234 # for example
On Windows that would look like
set SHDP_AMSERVICE_PORT 1234
Edit
Based on your comment I downloaded it and it's used in container-context.xml under
yarn/yarn/custom-amservice/src/main/resources/container-context.xml
yarn/yarn/batch-files/src/main/resources/container-context.xml
yarn/yarn/batch-partition/src/main/resources/container-context.xml
Nothing defines it, so it uses a default value. You can override it as above.

Pass properties to log4j before it loads

Is there a way to pass data or setting to log4j before it loads and then use that property within the config file.
I was assuming there is a system properties I could use:
log4j.appender.R.File=/usr/local/pfs/logs/${ws.host}/log4j.log
Where ws.host is the property I want to use.
But how can I set that value?
Also, I am in a web environment. How can I know at what point to set the property setting before log4j loads.
The default log4j PropertiesConfigurator supports variable substitution.
http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PropertyConfigurator.html
So, you could pass system properties like this "-DmyProject.logFile="/temp/test.log" to your Java startup, and then in the properties files have "log4j.appender.R.File=${myProject.logFile}".
If working from a web environment, you might want to check out Spring's Log4jConfigListener. It uses a listener (Servlet API 2.4+) to initialization log4j ahead of other components. Even if not using Spring, you should be able to use the source as an example to easily create your own listener.

Categories

Resources