I have a maven project with the following dependencies for logging:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
My properties file looks like this :
### Default logger configuration ###
log4j.rootLogger = INFO, fa
log4j.appender.fa = org.apache.log4j.FileAppender
log4j.appender.fa.File = logs/ServerLogs.log
log4j.appender.fa.layout = org.apache.log4j.PatternLayout
log4j.appender.fa.layout.ConversionPattern = %d{ISO8601} %p [%t] %c (%F:%L) - %m%n
### Set Settings for Hibernate Logging ###
log4j.logger.org.hibernate = ERROR
### Set Settings for Spring Framework Logging ###
log4j.logger.org.springframework= ERROR
The problem is that when I execute a statement like the following :
logger.info("Initializing DB connection for the first time");
This log message is written in the file that I specified in the properties file but it is also appended in the command line. How can I disable this behavior ? I want the message to printed only in the file , not in the command line .
Any ideas ?
I found out that this disables the log4j completely:
org.apache.log4j.LogManager.resetConfiguration();
org.apache.log4j.LogManager.getRootLogger().addAppender(new NullAppender());
If you still want the logging but directed to a file you can do this:
LogManager.resetConfiguration();
LogManager.getRootLogger().addAppender(new FileAppender(new PatternLayout(), "log.log"));
Related
Pom.xml file
<groupId>com.giffing.bucket4j.spring.boot.starter</groupId>
<artifactId>bucket4j-spring-boot-starter</artifactId>
<version>0.7.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>jcache</artifactId>
<version>2.8.2</version>
</dependency>
application-local.yml file
spring:
cache:
cache-names:
- rate-limit-buckets
caffeine:
spec: maximumSize=100000,expireAfterAccess=3600s
bucket4j:
enabled: true
filters:
- cache-name: rate-limit-buckets
filter-method: servlet
strategy: first
url: /patient
http-response-body: "{ \"status\": 429, \"error\": \"Too Many Requests\" }"
rate-limits:
- expression: getRemoteAddr()
bandwidths:
- capacity: 1
time: 30
unit: seconds
I have set the capacity to 1 and time to 30s so that I receive "Too Many Requests" error upon 2 api hits with in 30s, but I am not receiving any error or anything, basically bucket4j rate limiting is not even running as it seems.
Is my application-local.yml even being loaded? earlier I had the configurations in application-local.properties file and rate limiter was not working by that either, after exploring internet some people suggested using .yml file to load bucket4j configs so I did that too but even now it is not working.
Question 1:
I have been looking everywhere for an example of a log4j2.properties file that uses an appender to write to a file - but not the rolling appender.
I already roll the files through syslog and I don't understand why I have to define an entire separate rolling strategy just to write to a single file.
Is there such a beast?
Question 2:
If I can't get an answer for Question 1, then I have been looking for a comprehensive listing of all the appenders possible in a log4j2.properties file. I see plenty of examples of all the different rolling appenders, but I was looking for appenders that are not specific either to rolling or console.
All I've been able to find so far are properties files with rolling appenders, or non-rolling XML files.
(summary)
Either the answer to Question 1 or Question 2 would be amazing. The bottom line is that I'm looking for something that doesn't involve rolling, that would be put in a .properties file (specifically for Elasticsearch).
Thank you!
Question 1: I have an very simple log file with just log everything to a file - not rolling file.
name=PropertiesConfig
property.filename = logs
appenders = console, file
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{dd} [%t] %c{1} - %msg%n
appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName=${filename}/propertieslogs.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
loggers=file
logger.file.name=com.aavn.viking.feedback360
logger.file.level = info
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = LOGFILE
rootLogger.level = info
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT
About the rolling file strategy I think it is used for delete the log file when it meet specific business like over 30 days, the accumulated file over 100 MB, or something else.
Question 2: There are some appenders that you can put on log4j2.properties file except console, rolling file and file such as: CassandraAppender,FailoverAppender, FlumeAppender, JDBCAppender, JMS Appender, HttpAppender etc. The link below is for more information about another appenders.
http://logging.apache.org/log4j/2.x/manual/appenders.html
P/s: If you want run my log4j2.properties, you must addlogging.config=src/main/resources/log4j2.properties to the application.properties
and add log4j2 dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
I am running Spring Cloud Config Server and a Cloud Config Client in Pivotal Cloud Foundry .
1st version:
I can update client properties by pushing a YML file to a git repo. I did a POST on config-client/refresh and it updated successfully. I am connecting via HTTPS.
2nd version:
I am now trying to automatically push config updates with RabbitMQ. My Cloud Config Server successfully receives a gitlab webhook POST call to the config-server/monitor endpoint. Config Server sees the update and sends a message to my Config Client.
However, the problem is that the client never updates its properties. I don't think it knows that any of its property files are updated. [ It may not know specifically what file was updated... ]
Is what I'm trying to do even possible with gitlab's webhooks ?
Thank you for any help.
Client log entries with app context reload highlighted. This happens after the code push:
2017-02-03T20:53:59.27-0500 [APP/0] OUT 2017-02-04 01:53:59.274 INFO 19 --- [nio-8080-exec-5] o.s.c.c.monitor.PropertyPathEndpoint : Refresh for: *
2017-02-03T20:53:59.34-0500 [APP/0] OUT 2017-02-04 01:53:59.346 INFO 19 --- [nio-8080-exec-5] nfigurationApplicationContextInitializer : **Adding cloud service auto-reconfiguration to ApplicationContext**
2017-02-03T20:53:59.35-0500 [APP/0] OUT 2017-02-04 01:53:59.358 INFO 19 --- [nio-8080-exec-5] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext#3325da05: startup date [Sat Feb 04 01:53:59 UTC 2017]; root of context hierarchy
These are my dependencies:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus-parent</artifactId>
<version>1.3.0.M1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-monitor</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
my log4j setup is following. When I run as Boot App, the logs are correctly written in console, debug.log and dump.log. Below is what I do in my program to write log in console and debug.log
static final Logger LOG = Logger.getLogger(EnvironmentLoader.class);
LOG.info("blah blah!");
Below is what I do in my program to write log in dump.log
private static final Logger DUMP_LOG = Logger.getLogger("dumpLogger");
DUMP_LOG.info("blah blah!");
Both works fine if I run as Spring Boot App. If I package it as war and run in tomcat, DUMP_LOG writes correctly in dump.log but LOG is not writing in console or debug.log. I wonder why.
log4j.rootLogger=INFO, stdout, debugLog
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %-5p %c.%M:%L - %m%n
log4j.appender.stdout.Target=System.out
log4j.appender.debugLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.debugLog.Append=true
log4j.appender.debugLog.DatePattern='.'dd-MM-yyyy
log4j.appender.debugLog.File=${catalina.base}/logs/debug.log
log4j.appender.debugLog.MaxFileSize=10MB
log4j.appender.debugLog.encoding=UTF-8
log4j.appender.debugLog.layout=org.apache.log4j.PatternLayout
log4j.appender.debugLog.layout.ConversionPattern=%d %-5p %c.%M:%L - %m%n
log4j.category.debugLogger=DEBUG, debugLog
log4j.additivity.debugLogger=false
log4j.appender.dumpLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.dumpLog.Append=true
log4j.appender.dumpLog.DatePattern='.'dd-MM-yyyy
log4j.appender.dumpLog.File=${catalina.base}/logs/dump.log
log4j.appender.dumpLog.MaxFileSize=10MB
log4j.appender.dumpLog.encoding=UTF-8
log4j.appender.dumpLog.layout=org.apache.log4j.PatternLayout
log4j.appender.dumpLog.layout.ConversionPattern=%d - %m%n
log4j.category.dumpLogger=DEBUG, dumpLog
log4j.additivity.dumpLogger=false
The following dependency resolved the issue.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>
I would like to turn off log4j logging for Spring 3.1 while leaving things on debug for my own code.
I tried sticking this line into my log4j.properties:
log4j.category.org.springframework = WARN
To get this:
# Root Logger Setup: Includes the level of reporting and appenders -> where
# log messages get sent
log4j.rootLogger = DEBUG,ca,fa
log4j.category.org.springframework = WARN
#ca - Console Appender - Send messages to the console
log4j.appender.ca = org.apache.log4j.ConsoleAppender
log4j.appender.ca.layout = org.apache.log4j.PatternLayout
log4j.appender.ca.layout.ConversionPattern = [acme]: [%-5p] - %d{yyyy-MMM-dd HH:mm:ss} - %c{1}:%M(): %m%n
#fa - File Appender - Send messages to a log file
log4j.appender.fa = org.apache.log4j.RollingFileAppender
log4j.appender.fa.File = acme.log
log4j.appender.fa.MaxFileSize = 100KB
log4j.appender.fa.MaxBackupIndex = 10
log4j.appender.fa.Threshold = DEBUG
log4j.appender.fa.layout = org.apache.log4j.PatternLayout
log4j.appender.fa.layout.ConversionPattern = [%-5p] - %d{yyyy-MMM-dd HH:mm:ss} - %c{2}:%M(): %m%n
No luck in shutting off the debug output from Spring though.
Thanks in advance for any help
Steve
Are all your dependencies in place?
1.3.2.3 Using Log4J
Many people use Log4j as a logging framework for configuration and management purposes. It's efficient and well-established, and in fact it's what we use at runtime when we build and test Spring. Spring also provides some utilities for configuring and initializing Log4j, so it has an optional compile-time dependency on Log4j in some modules.
To make Log4j work with the default JCL dependency (commons-logging) all you need to do is put Log4j on the classpath, and provide it with a configuration file (log4j.properties or log4j.xml in the root of the classpath). So for Maven users this is your dependency declaration:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.0.RELEASE</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
<scope>runtime</scope>
</dependency>
</dependencies>
And here's a sample log4j.properties for logging to the console:
log4j.rootCategory=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n
log4j.category.org.springframework.beans.factory=DEBUG
I figured this out.
In my classpath I have a directory C:\Classes for convenience when I am experimenting with things. I had another log4.properties file there, which was superseding the one packaged in my WAR making it look like the technique below wasn't working. I renamed the log4.properties in my C:\Classes and all is well.
Thanks to everyone who took a look and thanks to the people at Spring Source who made doing this necessary. It is good to know that an extensive level of debugging is easily available to me when I want it instead of just getting a black box.