Does Logback SMTPAppender support environment variables? - java

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}"/>

Related

Set custom name for Log4j2 log level in XML configuration file

I'd like to force my log4j2 to display log level like "Warning" instead of "WARN" etc.
Is it possible to do so? It can't be done programmatically, it must be done in XML configuration file because I'm using it in an already built image so I don't have access to the source code.
I tried something like this but it did nothing.
<CustomLevels>
<CustomLevel name="Warning" intLevel="300" />
<CustomLevel name="Information" intLevel="400" />
</CustomLevels>
I'd like to replace default log levels with my custom names.
Edit:
I need something like this, but for JsonTemplateLayout:
<PatternLayout>
<Pattern>"%level{WARN=Warning, DEBUG=Debug, ERROR=Error, TRACE=Trace, INFO=Info}"</Pattern>
</PatternLayout>
It must be in JSON format using JsonTemplateLayout (no PatternLayout)
I found the solution! :)
<JsonTemplateLayout eventTemplateUri="classpath:EcsLayout.json">
<EventTemplateAdditionalField
key="#l"
format="JSON"
value='{"$resolver": "pattern", "pattern": "%level{WARN=Warning, DEBUG=Debug, ERROR=Error, TRACE=Trace, INFO=Information}"}'/>
</JsonTemplateLayout>
It's not very elegant, but it works :)
Edit:
I'm not sure why, but for ERROR level it didn't work correctly.
It changed ERROR to Error, but my #l contained also exception message.
I needed to add %ex{none} to my pattern like this:
{"$resolver": "pattern", "pattern": "%level{FATAL=Fatal, ERROR=Error}%ex{none}"}

Adding a custom field in log4j using SyslogAppender

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.

System.getProperty("some stuff")

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

vertx LoggerHandler not adding logback

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.

How to set values to placeholders in log4j.properties file?

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

Categories