I am using spring/hibernate application. i am using log4j for logging. the problem is i need to place a placeholder in log4j.properties file and i need to set the value to the place holder based on the environment(Dev, UAT or Production).
Environment=${environment}
is it possible? Please help me.
Thanks!
You can pass the variable and value either by command line or set environment variable like below.
-DEnvironment=dev
then you can use this in log file like :
Environment=${Environment}
Why not just use seperate keys, and pick the right one at runtime?
Environment.dev=Development
Environment.prod=Production
Environment.qa=QA
bundle.getKey(ENVIRONMENT_KEY_PREFIX + ".dev");
Related
I'm using the SyslogAppender in my java application and I'm trying to add a custom field to the resulting log. How can I add an additional field to my log4j.properties?
My current log4j.properties (the last line shows what I want to achieve):
log4j.appender.SYSLOG=org.apache.log4j.net.SyslogAppender
log4j.appender.SYSLOG.threshold=INFO
log4j.appender.SYSLOG.syslogHost=localhost
log4j.appender.SYSLOG.facility=LOCAL4
log4j.appender.SYSLOG.header=true
log4j.appender.SYSLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.SYSLOG.layout.conversionPattern=my-app: %m%n
log4j.appender.SYSLOG.applicationName=${STACKNAME}
The ${STACKNAME} is a system property configured by the deployment job, depending on the environment (e.g. prod, test, dev).
From this answer I get the answer: add the lines
log4j.appender.graylog2.additionalFields={'filed_name': 'field_value', 'field2_name': 'field2_value'}
log4j.appender.graylog2.addExtendedInformation=true
to add field_name and field2_name with values field_value and field2_value. The property addExtendedInformation=true indicates Graylog to add those fields to all log entries.
In a very huge code base I found the following code snippet System.getProperty("some stuff"). I tried to look the property in some of the .properties files though i couldn't find it. Do you guys have any ideas where to look the property anywhere else in the code file system?
You might want to look into "System.setProperty" in your work space:
// Modifying a system property
System.setProperty("java.io.tmpdir","c:\\var\\tmp");
// Adding my own properties
System.setProperty("program.name","Property Test");
System.setProperty("program.version","3.01");
I am trying to use LoggerHandler to log all incoming requests. I am using logback.xml to specify appenders. I am setting system property for logging.
System.setProperty("org.vertx.logger-delegate-factory-class-name",
"org.vertx.java.core.logging.impl.SLF4JLogDelegateFactory");
Still it is logging everything in console not in file.
This worked for me with Vert.x 3.4.1:
import static io.vertx.core.logging.LoggerFactory.LOGGER_DELEGATE_FACTORY_CLASS_NAME;
import io.vertx.core.logging.LoggerFactory;
// ...
setProperty (LOGGER_DELEGATE_FACTORY_CLASS_NAME, SLF4JLogDelegateFactory.class.getName ());
LoggerFactory.getLogger (LoggerFactory.class); // Required for Logback to work in Vertx
The key is to get a logger, which I guess initializes the Logging subsystem, the class that you use to get a Logger seems irrelevant as I tried with two different ones.
I run these lines as the first ones of the program in production code and in the tests to work properly in both contexts.
I was able to get it to work by setting the VM options as such:
-Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.Log4jLogDelegateFactory
Then in my log4j.properties, I had to add this:
log4j.category.io.vertx = TRACE
I know this question is getting a bit old, but the only way I was able to get the vertx LoggerHandler to not use JUL was to call LoggerFactory.initialise() after setting the system property as described in the question.
Even better, I set the property in my build.gradle, like so:
run {
systemProperty(
"vertx.logger-delegate-factory-class-name",
"io.vertx.core.logging.SLF4JLogDelegateFactory"
)
args = ['run', mainVerticleName, "--redeploy=$watchForChange", "--launcher-class=$mainClassName", "--on-redeploy=$doOnChange",
"-Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.SLF4JLogDelegateFactory" ]
}
And then at the very top of my MainVerticle::start I have:
LoggerFactory.initialise()
And, boom. Everything is now formatted correctly, including all the startup output.
Inside the definition for a Logback SMTPAppender, you can specify email configuration info like so:
<smtpHost>my.smtp.host</smtpHost>
<to>john.smith#example.com</to>
<from>no-reply#example.com</from>
<username>my_smtp_user</username>
<password>my_smtp_password</password>
<subject>%logger{20} - %m</subject>
Instead of hardcoding the <to>john.smith#example.com</to> field, I'd like it to pick up the local username of whatever account/machine the Java app is running on. For instance, if my Ubuntu username is ticketMonster, then I would like the <to> field to be:
<to>ticketMonster#example.com</to>
Or if my operating system username is bgates, I'd like it to be:
<to>bgates#example.com</to>
Hence, I'm looking for dynamic username evaluation. I tried:
<to>${username}#example.com</to>
But that does not work... Any ideas or suggestions here? Thanks in advance!
Variable substitution for the <to> element should work. If it does not, it's a bug. Could you please create a new issue on our jira so that this problem can be fixed?
I guess, There are few ways you can do it:
You can define your username in properties file and load your .properties file in logback.xml file.
property resource="filename.properties"
<username>${username}</username>
<password>${password}</password>
<to>${email1}</to>
<from>${from}</from>
You can use System variable like this in logback.xml file
< property name="user" value="${username}"/>
I use log4j for logging and i want to print all logger.debug statements in a particular class / selected package.
i set the cfg as below>
log4j.category.my.pkg=info
log4j.category.my.pkg.ab.class1=debug
but still only info messages are shown..
is this not the right way ?
Instead of using 'category' use 'logger'. Hence, these level are configured for entire log4j, and does not depend on appender, etc.
Following change works:
log4j.logger.my.pkg=info
log4j.logger.my.pkg.ab.class1=debug
Copying from my current log4j.properties:
log4j.logger.org.hibernate.tool.hbm2ddl=warn
log4j.logger.org.hibernate.sql=info