Can't add a rolling file appender with Log4j2.properties - log4j2

I have a java application running in a docker container. I can't change the program but I can change the log4j2.properties file. I want to add a rolling file appender but when I do it I get this error:
ERROR An exception occurred processing Appender file_appender java.security.AccessControlException: access denied ("java.io.FilePermission" "shared_logs" "read")
Then I gave all permissions to my shared_logs folder
chmod 777 shared_logs
But the error is still there.
This is how I added the the appender:
appender.fa.type = RollingFile
appender.fa.name = file_appender
appender.fa.fileName = shared_logs/elastic.log
appender.fa.filePattern = shared_logs/elastic-%d{yyyy-dd-MM}-%i.log.gz
appender.fa.layout.type = PatternLayout
appender.fa.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
appender.fa.policies.type = Policies
appender.fa.policies.time.type = TimeBasedTriggeringPolicy
appender.fa.policies.size.type = SizeBasedTriggeringPolicy
appender.fa.policies.size.size = 500
appender.fa.strategy.type = DefaultRolloverStrategy
appender.fa.strategy.max = 5
"fa" stands for fileappender

Well, you obviously still have a permissions problem. Your fileName is a relative file so where it is being created will be based on the currently directory of your app. That would typically be either the directory your app is in or the root directory. If it is the root directory and you are expecting it to be in your app this would explain the problem. If you add -Dlog4j2.debug to the startup options you would see where Log4j is trying to create the file.
Writing to log files in Docker Containers is also not usually considered a best practice. If you haven't already, I would suggest taking a look at Logging in the Cloud on the Log4j web site.

Related

Roll Logs Into Specific Directory Log4j2

Goal: Have Log4j2:
Create the root "logs" directory if it doesn't exist.
Create an "archive" directory if it doesn't exist so RollingFile appender can save the rolled logs.
Expected Result:
..\logs\MyLog.log
..\logs\archive\MyLog_2022-03-03_1.log
..\logs\archive\MyLog_2022-03-03_2.log
..\logs\archive\MyLog_2022-03-03_3.log
.
.
..\logs\archive\MyLog_2022-03-03_20.log
I'm expecting the creation of "logs" & "archive" directories in case they aren't there on start-up/1st roll.
Actual Results:
Regarding the "logs" directory:
if the "logs" directory doesn't exist prior to start-up then system continues running without logging anything or creating the directory.
if the "logs" directory exists prior to start-up then it saves the main log file to it.
Regarding the "archive" directory for rolling logs:
if the "archive" directory doesn't exist prior to start-up then it is created, the system crashes and an exception is thrown.
if the "logs" directory exists prior to start-up then the system crashes and an exception is thrown.
What I have tried:
Replacing the properties with their actual hard-coded values.
Replacing the path with "./" at the beginning.
Replacing the path with "../" at the beginning.
Replacing the beginning of the path with various possible Lookups from Log4j2's documentation.
Hard-coding the full-path, which worked but is not an option to use as it will change according to the customer's installation location.
Configuration:
<Properties>
<Property name="log.dir">logs</Property>
<Property name="log.file">MyLog</Property>
<Property name="pattern">%d %-5p [%t] %c{2}:%L - %m%n</Property>
</Properties>
<RollingFile name="MainLog" fileName="${log.dir}/${log.file}.log" filePattern="${log.dir}/archive/${log.file}_%d{MM-dd-yyyy}_%i.log.gz">
<PatternLayout pattern="${pattern}" />
<Policies>
<SizeBasedTriggeringPolicy size="20 MB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
Research:
I've tried various Google searches and looked into many Stackoverflow questions, I will not be listing them here as they do not include any actual relevant data. Weirdly enough, I wasn't able to find any similar question.
What am I missing?

Tomcat application server logs are not printed in a rolling file other than catalina.log using log4j2.properties

I have recently migrated application from log4j1.x to log4j2.17.1. Application logs are printed well in console as well as in Rolling file when start the tomcat server. I could see Tomcat server start up logs are all printing in catalina.log but the same server startup logs and any other server internal logs are not printed in rolling file. Rolling file prints only those logs are coming from class files. Could you anyone of you suggest what can be done to print all application server start up logs like some logs gets printed in console before server start or connection refused logs etc. also get printed into Rolling file? Same things was working well when I had log4j1.x version.It means when I had log4j1.x server start up logs are printed well in catalina as well as in a DailyRollingFileAppender but Now issue is only with log4j2, it means server start up logs are printed in catalain only not in a rolling file which I want. Please suggest.
Here is my log4j2.properties
rootLogger.level = info
property.filename =D:/log/TEST.log
appenders = R, console
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d %5p [%t] (%F:%L) - %m%n
appender.R.type = RollingFile
appender.R.name = File
appender.R.fileName = ${filename}
appender.R.filePattern = ${filename}.%d{yyyy-MM-dd}
appender.R.layout.type = PatternLayout
appender.R.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p [%15.15t]: %c{1}:%L - %m%n
appender.R.policies.type = Policies
appender.R.policies.time.type = TimeBasedTriggeringPolicy
appender.R.policies.time.interval = 1
rootLogger.appenderRef.console.ref = STDOUT
rootLogger.appenderRef.R.ref = File

apache flume log directory

Just begin to learn Apache Flume. I follow the instructions on Flume official getting started website:
https://cwiki.apache.org/confluence/display/FLUME/Getting+Started
Almost everything is fine after follow the instructions on above link. But I could not find any log file afterwards. I suppose log file is under {flume.directory}/logs. Any idea to find flume log files?
Here comes my log4j.properties:
flume.root.logger=INFO,LOGFILE
flume.log.dir=./logs
flume.log.file=flume.log
log4j.logger.org.apache.flume.lifecycle = INFO
log4j.logger.org.jboss = WARN
log4j.logger.org.mortbay = INFO
log4j.logger.org.apache.avro.ipc.NettyTransceiver = WARN
log4j.logger.org.apache.hadoop = INFO
log4j.logger.org.apache.hadoop.hive = ERROR
# Define the root logger to the system property "flume.root.logger".
log4j.rootLogger=${flume.root.logger}
# Stock log4j rolling file appender
# Default log rotation configuration
log4j.appender.LOGFILE=org.apache.log4j.RollingFileAppender
log4j.appender.LOGFILE.MaxFileSize=100MB
log4j.appender.LOGFILE.MaxBackupIndex=10
log4j.appender.LOGFILE.File=${flume.log.dir}/${flume.log.file}
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss,SSS} %-5p [%t] (%C.%M:%L) %x - %m%n
# Warning: If you enable the following appender it will fill up your disk if you don't have a cleanup job!
# This uses the updated rolling file appender from log4j-extras that supports a reliable time-based rolling policy.
# See http://logging.apache.org/log4j/companions/extras/apidocs/org/apache/log4j/rolling/TimeBasedRollingPolicy.html
# Add "DAILY" to flume.root.logger above if you want to use this
log4j.appender.DAILY=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.DAILY.rollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.DAILY.rollingPolicy.ActiveFileName=${flume.log.dir}/${flume.log.file}
log4j.appender.DAILY.rollingPolicy.FileNamePattern=${flume.log.dir}/${flume.log.file}.%d{yyyy-MM-dd}
log4j.appender.DAILY.layout=org.apache.log4j.PatternLayout
log4j.appender.DAILY.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss,SSS} %-5p [%t] (%C.%M:%L) %x - %m%n
# console
# Add "console" to flume.root.logger above if you want to use this
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.err
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d (%t) [%p - %l] %m%n
Cause your configuration use a relative path :
flume.log.dir=./logs
log4j.appender.LOGFILE.File=${flume.log.dir}/${flume.log.file}
Flume use your current directory to log.
(the directory where you are when you launch flume)
You can use an absolute path for flume.log.dir if you want to force flume to log in that path, no matter where you launch it.
Based on your log4j.properties, it would be in the same directory where you start your flume-agent, thats what ./ would do.
So, if you run from /home directory, it would be
/home/logs/flume.log
Also, you can use linux commands anytime to find that out,
find / -name flume.log ##search in whole disk
find . -name flume.log ##search in current directory

Setting java.io.tmpdir in Wildfly 8.2

I am attempting to set the location of the system property java.io.tmpdir to something other than the default "/tmp" in my standalone.xml file.
I have added the following element after the <extensions> element:
<system-properties>
<property name="java.io.tmpdir" value="/tmp/wildfly"/>
</system-properties>
However, when I start up wildfly, I see this in the log file:
java.io.tmpdir = /tmp
I don't see anywhere in any of the bin*.conf files or the bin*.sh files that's setting this... what am I missing?
Thanks.

Log4j configuration in Grails production environment with grails.config.locations

I am trying to maintain the log4j configuration in a separate file in production environment. I have this log4j.properties file (which in production resides in WEB-INF/classes):
log4j.rootLogger=error, stdout
log4j.rootLogger.additivity=false
log4j.logger.grails.app=info, stdout
log4j.additivity.grails.app=false
log4j.additivity.grails.app.service=false
log4j.logger.grails.app.controller=debug, stdout
log4j.additivity.grails.app.controller=false
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%7r] %6p - %14.14c - %m%n
I completely removed the log4j configuration from the Config.groovy. And according to the second option in this comment http://jira.codehaus.org/browse/GRAILS-2730?focusedCommentId=137021&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_137021 I added the location of log4j.properties in Config.groovy this way:
production {
grails.serverURL = "http://xxxxx.ru/${appName}"
grails.config.locations = [ "classpath:log4j.properties" ]
}
But when deploying the application I still get the exception about the stacktrace.log file:
log4j:ERROR setFile(null,true) call failed.
java.io.FileNotFoundException: stacktrace.log (Permission denied)
at java.io.FileOutputStream.openAppend(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:207)
at java.io.FileOutputStream.<init>(FileOutputStream.java:131)
at org.apache.log4j.FileAppender.setFile(FileAppender.java:294)
at org.apache.log4j.FileAppender.activateOptions(FileAppender.java:165)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
...
I do not understand why.. Anyone?
Thanks.
java.io.FileNotFoundException: stacktrace.log (Permission denied)
should mean that the user that Tomcat is running under does not have proper write permissions in the folder where Log4J tries to create the stacktrace.log file. By default, this is the folder that had been the working directory when Tomcat had been started.
You can specify a custom stacktrace.log location with the log4j.appender.stacktraces.File configuration option, like so:
log4j.logger.stacktraces.com.foo=INFO,stacktraces
log4j.additivity.stacktraces=false
log4j.appender.stacktraces=org.apache.log4j.DailyRollingFileAppender
log4j.appender.stacktraces.File=${log.dir}/fooStacktraces.log
log4j.appender.stacktraces.DatePattern=${roll.pattern.daily}
log4j.appender.stacktraces.layout=org.apache.log4j.PatternLayout
log4j.appender.stacktraces.layout.ConversionPattern=%d{${datestamp}}%p%m%n
By default, properties files (like any other resources that are not compiled) are not copied to the WEB-INF/classes folder.
To copy them, manually, create the file scripts/Events.groovy in your project and add the following code (assuming that your properties file is in the application root):
eventCompileEnd = {
ant.copy(todir:classesDirPath) {
fileset(file:"${basedir}/*.properties")
}
}

Resources