External Log4j.properties in not printing logs - spring boot 2.2.1 - spring

Develop a spring boot application in 2.2.1 RELEASE.Everything is working fine except Loging using log4j.properties.
In apoplication.properies, added logging.config as given below
logging.config =${external.config}/log4j.properties.
Pom.xml file,excluded spring-boot-starter-logging and added spring-boot-starter-log4j as given below
<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>
log4j.properties
log4j.rootLogger=error,Service
# Direct log messages to a log file
log4j.appender.Service=org.apache.log4j.RollingFileAppender
log4j.appender.Service.File=C:/log/Service.log
log4j.appender.Service.MaxFileSize=1MB
log4j.appender.Service.MaxBackupIndex=1
log4j.appender.Service.layout=org.apache.log4j.PatternLayout
log4j.appender.Service.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - [%X{AUDIT_USER}] %m%n
i referred following links in stack
1.Logger not printing with log4j.properties within Spring Boot 1.5.7
2.Spring boot logging with log4.properties file in not working
Edit 1
As Andy Wilkinson comments, i modified log4j.properties file content and file name, file name should be log4j2.properties.
I tried to log some string to log file as given below
public class ServiceMain {
private static final Logger logger = LogManager.getLogger(ServiceMain.class);
public static void main(String[] args) {
SpringApplication.run(ServiceMain.class, args);
logger.debug("----------------------Stating spring booot----------------------");
}
}
debug log "----------------------Stating spring booot----------------------" wont write to c:/log/service.log
Log file
2019-11-22 05:20:14,631 INFO o.s.b.StartupInfoLogger [main] Starting ServiceMain v7.0.0.0 on host-4 with PID 119176 (D:\Service\target\Service-1.0.jar started by Administrator in D:\Service\target)
2019-11-22 05:20:14,634 DEBUG o.s.b.StartupInfoLogger [main] Running with Spring Boot v2.2.1.RELEASE, Spring v5.2.1.RELEASE
2019-11-22 05:20:14,635 INFO o.s.b.SpringApplication [main] The following profiles are active: Service
2019-11-22 05:20:24,898 INFO o.s.b.StartupInfoLogger [main] Started ServiceMain in 12.312 seconds (JVM running for 14.189)
Did i miss any log4j2.properties configuration?

You are using Log4j 2 (which you should be as version 1 is no longer supported) but appear to be configuring it using a Log4j 1 configuration file.
You can learn more about Log4j 2’s configuration properties in its documentation. It includes this example:
status = error
dest = err
name = PropertiesConfig
property.filename = target/rolling/rollingtest.log
filter.threshold.type = ThresholdFilter
filter.threshold.level = debug
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %m%n
appender.console.filter.threshold.type = ThresholdFilter
appender.console.filter.threshold.level = error
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${filename}
appender.rolling.filePattern = target/rolling2/test1-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d %p %C{1.} [%t] %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 2
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=100MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 5
logger.rolling.name = com.example.my.app
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile
rootLogger.level = info
rootLogger.appenderRef.stdout.ref = STDOUT

Related

Routing console output to a log file in Springboot

I am trying to route the console logging to a file. Though, I was able to generate the log file but it's not capturing the output generated from logger.info . The output is getting printed on the console but the same is not reflecting in the log file.
application.properties :
logging.file.path= log
logging.pattern.file= [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
Code :
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
#Service
public class GetImageFiles {
#Value("${app.images.base.url}")
String imagesBaseUrl;
#Autowired
DataAccessor dataAccessor;
private static Logger logger = LoggerFactory.getLogger(GetImageFiles.class);
public void getImageDetails(String sessionID, String imageId, String articleId){
HashMap<String, String> map = new HashMap<>();
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("X-Chorus-Session",sessionID);
HttpEntity<String> request = new HttpEntity<>(null, headers);
String imageFilesUrl = imagesBaseUrl + "/" + imageId;
ResponseEntity<String> responseEntity = restTemplate.exchange(imageFilesUrl, HttpMethod.GET, request, String.class);
JSONObject jsonObject = new JSONObject(responseEntity.getBody());
String imageName = jsonObject.get("filename").toString();
JSONObject thumbnails = (JSONObject) jsonObject.get("thumbnails");
JSONObject thumbnailsSize = (JSONObject) thumbnails.get("large");
String imageUrl = thumbnailsSize.get("url").toString();
map.put(imageName, imageUrl);
for(Map.Entry<String, String > mapValue : map.entrySet()) {
if (mapValue.getKey().lastIndexOf(".") > 0) {
String imageMapKey = mapValue.getKey().substring(0, mapValue.getKey().lastIndexOf("."));
if (imageMapKey.equals(articleId)) {
logger.info("Processing : The image name is : " + mapValue.getKey() + " and the imager URL is :" + mapValue.getValue() + " for article Id is :" + articleId);
dataAccessor.updateImageDetails(mapValue.getKey(), mapValue.getValue(), articleId);
}
}
}
}
}
pom.xml :
<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.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.1</version>
</dependency>
Console output ..
2022-03-09 17:17:00.110 INFO 32900 --- [ restartedMain] o.s.b.w.e.t.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-03-09 17:17:00.122 INFO 32900 --- [ restartedMain] c.a.t.ThirdlightApplication : Started ThirdlightApplication in 11.293 seconds (JVM running for 12.775)
Total Article Ids to be processed :10
There is no image ids to be processed for article id :52KTM41
There is no image ids to be processed for article id :52LGY10
There is no image ids to be processed for article id :52NAZ41
There is no image ids to be processed for article id :52NBT60
There is no image ids to be processed for article id :52NEK40
There is no image ids to be processed for article id :52QAZ30
There is no image ids to be processed for article id :54LAQ01
There is no image ids to be processed for article id :54LAQ20
There is no image ids to be processed for article id :56NAK43
There is no image ids to be processed for article id :56NEU40
The process has been completed !
spring.log :
[INFO ] 2022-03-09 17:20:49.920 [restartedMain] ThirdlightApplication - Starting ThirdlightApplication using Java 11.0.7 on LHTU05CD9032TMM with PID 6456 (C:\Users\psingh69\Downloads\thirdlight\target\classes started by psingh69 in C:\Users\psingh69\Downloads\thirdlight)
[INFO ] 2022-03-09 17:20:49.933 [restartedMain] ThirdlightApplication - No active profile set, falling back to 1 default profile: "default"
[INFO ] 2022-03-09 17:20:50.000 [restartedMain] DevToolsPropertyDefaultsPostProcessor - Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
[INFO ] 2022-03-09 17:20:50.000 [restartedMain] DevToolsPropertyDefaultsPostProcessor - For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
[INFO ] 2022-03-09 17:20:59.846 [restartedMain] TomcatWebServer - Tomcat initialized with port(s): 8080 (http)
[INFO ] 2022-03-09 17:20:59.994 [restartedMain] ServletWebServerApplicationContext - Root WebApplicationContext: initialization completed in 9993 ms
[INFO ] 2022-03-09 17:21:00.670 [restartedMain] OptionalLiveReloadServer - LiveReload server is running on port 35729
[INFO ] 2022-03-09 17:21:00.723 [restartedMain] TomcatWebServer - Tomcat started on port(s): 8080 (http) with context path ''
[INFO ] 2022-03-09 17:21:00.742 [restartedMain] ThirdlightApplication - Started ThirdlightApplication in 11.588 seconds (JVM running for 13.107)
In my case I had to put the configuration file "logback.xml" inside the folder src/main/resources...
It is highly customizable but you can start with something like this (both STDOUT - the console - and a FILE are simultaneously written):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE logback>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date{yyyy-MM-dd HH:mm:ss} [%thread] %level %logger{0} - %msg \(%file:%line\)%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/yourappname.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logs/yourappname.%d{yyyy-MM-dd}.log.tar.gz</fileNamePattern>
<maxHistory>7</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd'T'HH:mm:ss'Z'} - %m%n</pattern>
</encoder>
</appender>
<root level="INFO" additivity="false">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
The example above writes an "yourappname.log" file inside the "/logs" folder (C:\logs\yourappname.log if you're using Windows and your app is running on drive C:, on /logs/yourappname.log if you're in Linux or MacOS).
Furthermore, the above configuration file provides a time-based LogRotation mechanism (which means that, at regular time intervals - 7 days in this case - the logfile is compressed and archived in a tar.gz fashion and a new one is started fresh.)
I have no idea about the opportunity to put that configuration string inside the application.properties, sorry...
Slightly different from yours, my pom.xml refers to an alternative log framework (ch.qos.logback), here it is:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.11</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.11</version>
</dependency>

Why I only can print out "Error" from log4j2?

Part of my code:
public class TestContext {
private static TestContext textContext = null;
private WebDriver driver;
static ConfigFileReader configFileReader = new ConfigFileReader();
private static Logger log = LogManager.getLogger(TestContext.class.getName());
public static void main(String[] args)
{
log.debug("Debug Message Logged !!!");
log.info("Info Message Logged !!!");
log.error("Error Message Logged !!!");
}
In console when I ran it:
17:17:49.037 [main] ERROR Util.TestContext - Error Message Logged !!!
Properties file:
status = error
name = PropertiesConfig
#Make sure to change log file path as per your need
property.filename = C:/logs/debug.log
filters = threshold
filter.threshold.type = ThresholdFilter
filter.threshold.level = debug
appenders = rolling
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${filename}
appender.rolling.filePattern = debug-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 20
loggers = rolling
#Make sure to change the package structure as per your application
logger.rolling.name = com.howtodoinjava
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile
Where the configuration file:
enter image description here
Dependencies:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.14.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.14.1</version>
</dependency>
is it because it can not locate the properties file???
Problem is solved when I put the properties file under src/test/resources directly.

Getting a Java 8 application to log to catalina.out with log4j2

This is a personal project and I just want this to work.
I have a Java 8 application that is using Log4j 2 and all I want to do is get it to log to one of the general Tomcat 9 logs like catalina.out or localhost or the tomcat stdout log. Nothing I do seems to work.
Log4J is included in the war file, I only need it to work for this one application under Tomcat 9. I do not want to move to another logging library. This is my log4j.properties file.
log4j.rootCategory=debug,console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.out
log4j.appender.console.immediateFlush=true
log4j.appender.console.encoding=UTF-8
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.conversionPattern=%d [%t] %-5p %c - %m%n
This is an example of how I'm using this in my code:
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
public final class DataController {
private static Logger LOG = LogManager.getLogger(DataController.class);
public void exampleMethod() {
LOG.info("Log something");
}
}
Nothing shows up in any log. Tomcat 9 is a stock install and has a stock logging.properties file. This is NOT a Spring application. It is pure Java Servlets and POJO. For further context, I did originally use a file appender and logged to a custom file. That didn't work either. By the way, although I know some of the terminology, I am a complete noob at Java.
I was using a log4j 1.2 properties file. This sample properties file worked.
status = debug
name = PropertiesConfig
filters = threshold
filter.threshold.type = ThresholdFilter
filter.threshold.level = debug
appenders = console
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT

Spring Boot Web Socket RabbitMQ not available

I have simple spring boot app with web socket configuration.
When i run my app with SimpleBroker(provided by spring) everything works fine, but when i want to use rabbitmq instead of SimpleBroker, i have some problems, my broker is "not available"
#Configuration
#EnableWebSocketMessageBroker
open class WebSocketConfig : ILogging by LoggingImp<WebSocketConfig>(),
WebSocketMessageBrokerConfigurer {
#Autowired
private lateinit var env: Environment
override fun registerStompEndpoints(registry: StompEndpointRegistry) {
registry.addEndpoint("/hig").setAllowedOrigins("*").withSockJS()
}
override fun configureMessageBroker(registry: MessageBrokerRegistry) {
registry.setApplicationDestinationPrefixes("/app")
val host = env.getProperty("spring.rabbitmq.host")!!
val port = env.getProperty("spring.rabbitmq.port")!!.toInt()
val login = env.getProperty("spring.rabbitmq.username")!!
val pass = env.getProperty("spring.rabbitmq.password")!!
log.debug("webSocket=$host, $port, $login, $pass")
// registry.enableSimpleBroker("/chat")
registry.enableStompBrokerRelay("/chat")
.setRelayHost(host)
.setRelayPort(port)
.setClientLogin(login)
.setClientPasscode(pass)
.setAutoStartup(true)
.setSystemHeartbeatReceiveInterval(10000)
.setSystemHeartbeatSendInterval(10000)
}
}
response from WebSocketMessageBrokerStats:
{
"loggingPeriod": 1800000,
"webSocketSessionStatsInfo": "0 current WS(0)-HttpStream(0)-HttpPoll(0), 0 total, 0 closed abnormally (0 connect failure, 0 send limit, 0 transport error)",
"stompSubProtocolStatsInfo": "processed CONNECT(0)-CONNECTED(0)-DISCONNECT(0)",
"stompBrokerRelayStatsInfo": "1 sessions, localhost:5672 (not available), processed CONNECT(1)-CONNECTED(0)-DISCONNECT(0)",
"clientInboundExecutorStatsInfo": "pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0",
"clientOutboundExecutorStatsInfo": "pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0",
"sockJsTaskSchedulerStatsInfo": "pool size = 1, active threads = 0, queued tasks = 1, completed tasks = 0"
}
Broker details:
2018-02-15 23:03:56.367 [XNIO-2 task-16] DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'dispatcherServlet' processing GET request for [/hig/websocket]
2018-02-15 23:03:56.367 [XNIO-2 task-16] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /hig/websocket
2018-02-15 23:03:56.368 [XNIO-2 task-16] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Did not find handler method for [/hig/websocket]
2018-02-15 23:03:56.368 [XNIO-2 task-16] DEBUG o.s.w.s.s.s.WebSocketHandlerMapping - Matching patterns for request [/hig/websocket] are [/hig/**]
2018-02-15 23:03:56.368 [XNIO-2 task-16] DEBUG o.s.w.s.s.s.WebSocketHandlerMapping - URI Template variables for request [/hig/websocket] are {}
2018-02-15 23:03:56.368 [XNIO-2 task-16] DEBUG o.s.w.s.s.s.WebSocketHandlerMapping - Mapping [/hig/websocket] to HandlerExecutionChain with handler [org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler#352c1b98] and 1 interceptor
2018-02-15 23:03:56.368 [XNIO-2 task-16] DEBUG o.s.web.servlet.DispatcherServlet - Last-Modified value for [/hig/websocket] is: -1
2018-02-15 23:03:56.369 [XNIO-2 task-16] DEBUG o.s.w.s.s.t.h.DefaultSockJsService - Processing transport request: GET http://localhost:8080/hig/websocket
2018-02-15 23:03:56.370 [XNIO-2 task-16] DEBUG o.s.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2018-02-15 23:03:56.370 [XNIO-2 task-16] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request
2018-02-15 23:03:56.371 [XNIO-2 task-16] DEBUG o.s.w.s.h.LoggingWebSocketHandlerDecorator - New StandardWebSocketSession[id=Enl3glxP29UhO95gUafWViRXrXaZn3Kv556EVxW4, uri=/hig/websocket]
2018-02-15 23:03:56.374 [clientOutboundChannel-7] DEBUG o.s.w.s.a.NativeWebSocketSession - Closing StandardWebSocketSession[id=Enl3glxP29UhO95gUafWViRXrXaZn3Kv556EVxW4, uri=/hig/websocket]
2018-02-15 23:03:56.374 [clientOutboundChannel-7] DEBUG o.s.w.s.h.LoggingWebSocketHandlerDecorator - StandardWebSocketSession[id=Enl3glxP29UhO95gUafWViRXrXaZn3Kv556EVxW4, uri=/hig/websocket] closed with CloseStatus[code=1002, reason=]
2018-02-15 23:03:56.374 [clientOutboundChannel-7] DEBUG o.s.w.s.m.SubProtocolWebSocketHandler - Clearing session Enl3glxP29UhO95gUafWViRXrXaZn3Kv556EVxW4
2018-02-15 23:03:56.375 [clientOutboundChannel-8] DEBUG o.s.w.s.m.SubProtocolWebSocketHandler - No session for GenericMessage [payload=byte[0], headers={simpMessageType=OTHER, stompCommand=ERROR, nativeHeaders={message=[Broker not available.]}, simpSessionId=Enl3glxP29UhO95gUafWViRXrXaZn3Kv556EVxW4}]
For client i'm using angular 5
When i'm using registry.enableSimpleBroker("/chat") everything works fine
When i enable registry.enableStompBrokerRelay("/chat")... i'm getting:
message:Broker not available.
content-length:0
pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<version>2.0.0.M7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
<version>2.0.0.M7</version>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
<version>3.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-net</artifactId>
<version>2.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>io.projectreactor.ipc</groupId>
<artifactId>reactor-netty</artifactId>
<version>0.7.3.RELEASE</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.21.Final</version>
</dependency>
You are trying to connect using stomp protocol on a rabbitmq which doesn't run the stomp adapter.
Run:
rabbitmq-plugins enable rabbitmq_stomp
to enable stomp.
Also, you are connecting to the port 5672 which is the amqp protocol / adapter. This is why you have this stompCommand=ERROR, nativeHeaders={message=[Broker not available.]}.
Once enabled, the stomp adapter will by default listen on tcp/61613.

Tomee - slf4j create logs file but not writing on them

I want to use log4j for my app (I don't need to handle the server logs for now). I searched for a solution but all post are related with server logs. When I run the app, the log4j.properties is read and the logs file are created but it didn't write on them.
I'm using Tomee as server. I have other project with Wildfly using the same solution for log4j and it works.
log4j.properties:
log4j.appender.TEST = org.apache.log4j.DailyRollingFileAppender
log4j.appender.TEST.File = /opt/share/test-project/logs/test.log
log4j.appender.TEST.DatePattern = '.'yyyy-MM-dd
log4j.appender.TEST.layout = org.apache.log4j.PatternLayout
log4j.appender.TEST.layout.ConversionPattern = [%d{ISO8601}] %5p - %x - %c.%M(%L): %m%n
# Define the types of logger and level of logging
#log4j.rootLogger.org = DEBUG,CONSOLE
log4j.logger.com.test=DEBUG, TEST
Log4jConfig class which is initialized:
import java.io.File;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.log4j.PropertyConfigurator;
#WebListener
public class Log4jConfig implements ServletContextListener {
private final static String LO4G_FILENAME="/opt/share/test-project/config/log4j.properties";
private static final Logger LOGGER = LoggerFactory.getLogger(Log4jConfig.class);
...
...
public void contextInitialized(ServletContextEvent arg0)
{
try{
File file = new File(LO4G_FILENAME);
if (file.exists())
{
PropertyConfigurator.configure(LO4G_FILENAME);
LOGGER.info("[Log4JInitServlet - contextInitialized] - Log4J configured:"+LO4G_FILENAME);
}
}catch (Exception e) {
LOGGER.error("[Log4JInitServlet - contextInitialized] - Exception >>",e);
}
}
}
Test.java:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.Gson;
#Path("/test")
public class TestWs {
private static final Logger LOGGER = LoggerFactory.getLogger(TestWs.class.getName());
//here LOGGER is null. If I put it into test() is not null but not write on the log either.
#GET
#Path("/")
#Produces(MediaType.APPLICATION_JSON)
public Response test(){
LOGGER.debug("[TestWs - init] - init");
long currentSystemTime = System.currentTimeMillis();
LOGGER.error("[TestWs - test] - Error: TEST");
LOGGER.debug("[TestWs - test] - Finish Timing:"+(System.currentTimeMillis()-currentSystemTime));
return Response.ok().build();
}
}
POM.xml:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>1.7.12</version>
</dependency>
Any idea?
upgrade dependency of slf4f :
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.22</version>
</dependency>
use private static final Logger logger = LoggerFactory.getLogger(class-name.class); into class in which you want to create logs.
use bellow log4j configuration :-
Root logger option
log4j.rootLogger=INFO, file
Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./logs/log.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.logger.org.springframework=WARN
log4j.logger.com.amstech=DEBUG
I'm not sure of all the setup but if you put log4j lib/config in the container you can need to set in conf/system.properties:
openejb.logger.external=true
otherwise (for legacy reasons), tomee will setup log4j in a custom way making your configuration not respected. This property just means "use normal log4j behavior".

Resources