spring boot .war tomcat application logs not there - java

I am trying to write to my webapp logs to a .log file in the /logs directory in tomcat but the file is not generated nor is any logging output going to the console other than the spring logs and tomcat logs. When I run spring boot as a jar file with the embedded tomcat it writes to the log file just fine, but as soon as I deploy to tomcat via the webapps folder the application logs are no where to be found.
SpringBoot 2.1.2
Java 1.8
Tomcat 8.5
I have tried:
configuring LOGGING_CONFIG in setenv.sh
Multiple loggers.. logback, java utils etc..
application.properties:
logging.file=../logs/my-app.log
logging.level.org.springframework=INFO
logging.level.com.bose=DEBUG
log4j.properties for log4j:
log4j.rootLogger=${marge.log.level}, stdout, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=marge.log
#when stdout is also specified it will not write to the file
log4j.appender.file.MaxFileSize=1MB
# Keep one backup file
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss,SSS} [%c] [%-5p] %n%m%n
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# stdout uses PatternLayout
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss,SSS} [%c] [%-5p] %n%m%n
# Print only messages of level DEBUG or above in the package com.bose
log4j.logger.com.app=${log.level}
Expected: when I deploy my webapp in /webapps the application logs (generated by log4j) should be in my-app.log in the /logs directory
Actual: No file is generated and no logs are even in stdout/console

By default spring boot uses logback as a logging binder, so the key concept here to exclude logback first then include log4j
Example:
<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>
After that add log4j 2 dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
Add your log4j2.properties file to src/main/resources to be on classpath
Finally pay attention to what logging interface you are using this is important, with the above configuration you should use apache logging like:
package com.example;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
#SpringBootApplication
public class Application extends SpringBootServletInitializer {
private static final Logger LOGGER = LogManager.getLogger(Application.class);
public static void main(String[] args){
ApplicationContext ctx = SpringApplication.run(Application.class, args);
LOGGER.info("Info level log message");
LOGGER.debug("Debug level log message");
LOGGER.error("Error level log message");
}
}

Related

Log4j does not record any log file for my project

My log4j does not record any log file. I am not sure if this is a problem from the settings inside log4j.properties or the location of this file.
The way I built the project:
git clone https://github.com/spring-guides/gs-maven.git
Then modified: .\initial\pom.xml and added support for log4j:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-bom</artifactId>
<version>2.17.2</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
</dependencies>
just before:
</project>
Added log commands to .\initial\src\main\java\hello\HelloWorld.java as follows:
package hello;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class HelloWorld {
private static Logger logger = LogManager.getLogger(HelloWorld.class);
public static void main(String[] args) {
logger.error("Application status : {} ", "start");
Greeter greeter = new Greeter();
System.out.println(greeter.sayHello());
logger.error("Application status : {} ", "terminated");
}
}
Then added log4j.properties:
appender.stdout.type = Console
# ... other appender properties
appender.file.type = File
# ... other appender properties
logger.app = INFO, stdout, file
# logger.app.name = com.example.app
# is equivalent to:
# appender.stdout.type = Console
# appender.stdout.name = stdout
# ...
appender.file.type = File
appender.file.name = mylog
# ...
# logger.app.name = com.example.app
logger.app.level = INFO
logger.app.appenderRef.$1.ref = stdout
logger.app.appenderRef.$2.ref = file
I kept changing the location of this file to
.\initial\log4j.properties
.\initial\target\log4j.properties
.\initial\target\classes\log4j.properties
.\initial\target\classes\hello\log4j.properties
None of them have ever worked at all. I do not see mylog or any other log files.
The way I run the project is as follows:
cd initial
mvn compile
mvn package
java -jar target/gs-maven-0.1.0.jar
How should I fix this?
There are some problems with your configuration:
As Gopinath remarked, the configuration file should be in src/main/resources. Maven will copy it to target/classes,
Log4j 1.x uses the name log4j.properties, Log4j 2.x uses log4j2.properties,
The properties configuration format is the most difficult to master. I would advise you to use any of the other format (e.g. XML, which does not require additional dependencies).
If you insist on using the properties format, here are some hints:
every configuration requires a root logger, which is the ancestor of each logger and provides the default values for the other logger configurations. You can use the shorthand notation:
rootLogger = INFO, stdout, file
introduced in version 2.17.2 or the long notation:
rootLogger.level = INFO
rootLogger.appenderRef.$1.ref = stdout
rootLogger.appenderRef.$2.ref = file
The names of the appender references must match the name properties of the appenders:
appender.stdout.name = stdout
appender.file.name = file
there is no shorthand for that.
The FileAppender needs a fileName:
appender.file.fileName = mylog

Logging : Log4j2 Implementation using log4j2.properties file in Spring Boot

Logging is working fine if i specify
'logging.config = src/main/resources/log4j2.properties'
in my application.properties file.
Is there any other work around where spring boot automatically detects log4j2.properties and doesnot require to specify 'logging.config = src/main/resources/log4j2.properties' inside application.properties file..?
Spring Boot automatically detects log4j2.xml, log4j2.json files in classpath, but not in case log4j2.properties file, in my case
my 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.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
log4j2.properties:
name=PropertiesConfig
appenders = console, file
appender.console.type = Console
appender.console.name = ConsoleAppender
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{MM:dd HH:mm:ss.SSS} [%t]
[%level] [%logger{36}] - %msg%n
appender.file.type = File
appender.file.name = FileAppender
appender.file.fileName=/home/ubuntu/application.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern= %d{MM:dd HH:mm:ss.SSS} [%t] [%level]
[%logger{36}] - %msg%n
loggers=file
logger.file.name=com.project
logger.file.level = debug
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = FileAppender
rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = ConsoleAppender
Note : Spring boot version i am using is 2.1.3.RELEASE
Reference : https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html
I am not aware that Spring Boot modifies the logic Log4j 2 uses to locate configuration files. In fact, I have been working on a Spring Boot service that was using a log4j2.yml. I replaced that with a log4j2.properties and it worked fine. Log4j's normal discovery process finds it on the class path.
I am actually surprised that specifying logging.config=src/main/resources/log4j2.properties worked for you as a Spring Boot jar wouldn't normally have the "src" directory in it.
also can use this way:
java -Dlog4j.configurationFile=log4j2.xml -jar xxxx-app.jar
Reference:
https://logging.apache.org/log4j/2.x/manual/configuration.html

Redirect java.util.logging to log4j

I'm using an external JAR library that creates a log file every time is used:
private void initLogger() {
try {
boolean var1 = true;
FileHandler var2 = new FileHandler("lib.log", 4096000, 1, var1);
var2.setFormatter(new SimpleFormatter());
this.logger = Logger.getLogger("POS");
this.logger.addHandler(var2);
} catch (IOException var3) {
var3.printStackTrace();
}
}
This, create the files lib.log , lib.log.lck, etc...
The problem is that my App is using log4j(from maven xml):
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
What I want is simple, I want to redirect all the calls of that lib log to my logger.
I've tried using jul-to-slf4j :
https://www.slf4j.org/legacy.html
<!-- https://mvnrepository.com/artifact/org.slf4j/jul-to-slf4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>1.7.25</version>
</dependency>
With this configuration (console + daily file):
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1, RollingAppender
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%-5p] %d %c - %m%n
log4j.appender.RollingAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RollingAppender.File=pos.log
log4j.appender.RollingAppender.DatePattern='.'yyyy-MM-dd
log4j.appender.RollingAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.RollingAppender.layout.ConversionPattern=[%-5p] %d %c - %m%n
And then put this code at the beginning:
LogManager.getLogManager().reset();
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
Logger.getLogger("POS").setLevel(FINEST);
With this, the file is created (lib.log) but it's empty. And the output doesn't go to pos.log ( the one configured in log4j).
What I'm missing? Thanks!
You did not include slf4j-log4j12 in your project (more).
Maybe Log4j JDK Logging Adapter is better solution.

log4j does not create a log file in the system

I am creating a Spring Boot Application in STS (Spring Tool Suite).
The problem occurs when trying to log using log4j. It does not create the log file in the system, though it does log on the console. I tried various methods given in the forum(including checking case-sensitivity, , but I think I am going wrong somewhere. An elaborate response would be really helpful.
I have a dependency in the pom.xml file for the log4j jar:-
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
In my log4j.properties file, I am trying to have logs in console as well as a file
# Root logger option
log4j.rootLogger=ALL, stdout, file
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Redirect log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\log4j.log
log4j.appender.file.ImmediateFlush=true
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.FILE.layout.conversionPattern=%m%n
log4j.appender.file.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Java controller where I want to log (IndexController.java)
package com.project.Controller;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class IndexController {
final static Logger logger = Logger.getLogger(IndexController.class.getName());
#RequestMapping("/")
String index(Model model) {
logger.info("in index");
return "index";
}
}
Please comment if you need any further details. Thanks
Create a dependency for spring-boot-starter-log4j and exclude spring-boot-starter-logging in your pom.xml.Make the following entry in your 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.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>
This should create the log file and log the details.

Trace for multiple Camel Context

I have two projects let's say - Project_1 and Project_2, and corresponding camel contexts, Configured as follows
<camel:camelContext id="project_1.context" trace="true">
<camel:contextScan />
</camel:camelContext>
<camel:camelContext id="project_2.context" trace="true">
<camel:contextScan />
</camel:camelContext>
with corresponding deployment units as project_1.jar, and project_2.jar, have been deployed in jboss.
But still I am not able to see traces for any of the CamelContext while starting my jboss.
log configuration for both projects are:
log4j.rootLogger=ERROR, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%c] %m%n
log4j.logger.org.apache.camel=ALL
both projects have added the dependency for slf4j
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
both projects have added the module dependency for slf4j in jboss-deployment-structure:
<module name ="org.slf4j"/>
Kindly help me to let me know the thing that I am missing.
Looks like your root logger is set as "Error".
In your project, if you have the messages logged in DEBUG or INFO mode, please try to change the root log level to "DEBUG" or "INFO" and verify

Categories