Log file created; But no logs are getting stored - eclipse

IDE: STS(Eclipse)
Query #1:
I have created a SpringsMVC Project using Template available in STS tool(New->Springs Project-> Springs MVC Project). I am trying to use log4j. My problem is a new log file gets created in the specified location. But it remains empty and logs are not getting stored.
File: web.xml
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
File: log4j.properties
log4j.rootLogger = DEBUG, FILE
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=D:/log.out
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
Query #2:
Also in the provided template there is already log4j.xml file in src/main/resources folder (a standard with resp to Maven web app). But modifying that file alone has no change in log file storage.

By Default when STS create template and log4j then root logger defined for console appender.
You need to change for appending the log in file.
Define file appender first in log4j.xml.
<appender name="file" class="org.apache.log4j.FileAppender">
<param name="File" value="E:\\util.log" />
<param name="Append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%t %-5p %c{2} - %m%n"/>
</layout>
</appender>
Then make change in root level.
<!-- Root Logger -->
<root>
<priority value="debug" />
<appender-ref ref="file" />
</root>
It will work.

Related

Play framework (2.3.7) not using my logback configuration

I am using Play 2.3.7
I read the documentation here
https://www.playframework.com/documentation/2.3.x/SettingsLogger
I created a file called application-logger.xml (and as logger.xml) under my conf directory
<configuration>
<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />
<conversionRule conversionWord="coloredLevel" converterClass="play.api.Logger$ColoredLevel" />
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${application.home}/logs/foo.log</file>
<encoder>
<pattern>%date - [%level] - from %logger in %thread %n%message%n%xException%n</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%coloredLevel %logger{15} - %message%n%xException{5}</pattern>
</encoder>
</appender>
<logger name="play" level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</logger>
<logger name="application" level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</logger>
<root level="off" />
</configuration>
Now I run my play application activator clean run and it prints nothing on the console if there is an error. there is no file 'foo.log' created as well.
Based on my reading of the documentation, I thought all my application code will use a logger called "application" and the play internal would use a logger called "play" so if I define appenders for these two loggers I will have everything on console and my file.
But that does not seem to be happening.
I have put the application-logger.xml file in conf directory next to application.conf file.
I also tried renaming my file to logger.xml but still it is just ignored.
Edit::
I even tried to run my application like
./activator -Dlogger.resource=logger.xml run
./activator -Dlogger.file=./conf/logger.xml run
But nothing can make the play framework pick up that file. nothing.
I was able to find a solution here
https://www.reddit.com/r/scala/comments/6q6vb3/why_is_it_such_a_big_deal_for_play_framework_to/
Basically when launching the app use the logback configuration setting
so activator -Dlogback.configurationFile=logger.xml run`
The play documentation setting -Dlogger.resource or -Dlogger.file does not work.
This can be the case with custom ApplicationLoader, it require update load with
LoggerConfigurator(context.environment.classLoader).foreach {
_.configure(context.environment, context.initialConfiguration, Map.empty)
}
https://www.playframework.com/documentation/2.6.x/SettingsLogger#Using-a-custom-application-loader
I run it from IntelliJ, and with
-Dlogger.resource=logback-test.xml works smoothly.

Debug Gatling Scala Code in Intellij with Maven Java Project

I didn't add a comment in this Debug Scala Post because in my opinion it was another setup.
I had a similiar problem with Maven instead of SBT. I used Maven because the complete Java project was built upon it and I only wanted to debug my Gatling Scala code. However I'm not able to debug the code with the IDE.
Here's what I tried:
Clean the built mvn clean install
Invalidate caches in IDEA with the menu button
Use println to see whether the code is reached - worked
Redeploy the project on the Glassfish
Delete the generated files in the Glassfish and redeploy
This is my current setup:
Maven 3.3.9
Glassfish Payara 4 build 163
IDEA 2016.3.4. built on January 31 2017
JDK 1.8.0_51
Gatling 2.2.3
Scala-Maven-Plug 3.2.2
My solution is as follows.
With the help of colleague I figured out that the main class in Engine from the Gatling tutorial can be used to debug out of Intellij.
I basically built the whole archetype and moved the following files into the corresponding folders:
src/test/resources/gatling.conf
src/test/scala/Engine.scala and IDEPathHelper
That did the trick for me.
If anybody is finding hard to this just create a logback.xml file under src/test/resources and mention the following according to your need:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%-5level] %logger{15} - %msg%n%rEx</pattern>
<immediateFlush>false</immediateFlush>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>../gatling.log</file>
<append>true</append>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%-5level] %logger{15} - %msg%n%rEx</pattern>
</encoder>
</appender>
<!-- Uncomment for logging ALL HTTP request and responses -->
<!-- <logger name="io.gatling.http.ahc" level="TRACE" /> -->
<!-- <logger name="io.gatling.http.response" level="TRACE" /> -->
<!-- Uncomment for logging ONLY FAILED HTTP request and responses -->
<logger name="io.gatling.http.ahc" level="DEBUG" />
<logger name="io.gatling.http.response" level="DEBUG" />
<root level="WARN">
<appender-ref ref="CONSOLE" />
</root>
<root level="WARN">
<appender-ref ref="FILE" />
</root>
</configuration>

Spring boot and JBoss 8 Wildfly log configuration application

I have a Spring boot application prepare to be a WAR. It deploys without problems on Tomcat 8 (embedded or standalone) as well as on JBoss 8 Wildfly.
But while on Tomcat we have had configured working logback configuration on JBoos it does not work any more.
I have tried few different suggested solutions:
https://stackoverflow.com/a/21887529/3997870
https://stackoverflow.com/a/23080264/3997870
The best which I have found was to add to my project WEB-INF/jboss-deployment-structure.xml with
<jboss-deployment-structure>
<deployment>
<!-- exclude-subsystem prevents a subsystems deployment unit processors running on a deployment -->
<!-- which gives basically the same effect as removing the subsystem, but it only affects single deployment -->
<exclude-subsystems>
<subsystem name="logging" />
</exclude-subsystems>
</deployment>
</jboss-deployment-structure>
but still it does not solve problem completely. In the logs I have same line twice (not because of logback configuration because on Tomcat worked properly) and also double info about time, level, thread was printed in first record.
[2014-11-26 15:28:42,605] [INFO ] [MSC service thread 1-3 ] [stdout] [NONE ] [2014-11-26 15:28:42.605 INFO 8228 --- [vice thread 1-3] o.s.boot.SpringApplication : Starting application on LCJLT306 with PID 8228 (started by Piotr.Konczak in D:\servers\wildfly-8.2.0.Final\bin)
]
[2014-11-26 15:28:42,605] [INFO ] [MSC service thread 1-3 ] [o.s.boot.SpringApplication] [NONE ] [Starting application on LCJLT306 with PID 8228 (started by Piotr.Konczak in D:\servers\wildfly-8.2.0.Final\bin)]
As you can see in above example first record contains somehow additional timestamp, level and thread (I guess add by Wildfly during some redirect) while the second line is proper and expected.
My logback config has 2 parts - 1st inside app and 2nd outside app to make it possible to reconfigure in environments).
Inside the classpath:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<include file="/opt/appName/config/appNameLogConfig.xml" />
</configuration>
Outside the app (included one):
<?xml version="1.0" encoding="UTF-8"?>
<included>
<property name="DESTINATION_FOLDER" value="/opt/appName/logs" />
<property name="FILE_NAME" value="AppName" />
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${DESTINATION_FOLDER}/${FILE_NAME}.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--daily rollover-->
<fileNamePattern>${DESTINATION_FOLDER}/${FILE_NAME}.%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%-5level %date %-30thread %-30logger{30} [%-10mdc{requestId:-NONE}] %msg%n</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="INFO"/>
<logger name="org.hibernate" level="INFO"/>
<logger name="com.wordnik" level="INFO"/>
<logger name="com.mangofactory" level="INFO"/>
<logger name="com.company.appName" level="INFO"/>
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</included>
Does anyone see possible reason or misconfiguration?
I know it is a little bit late but, if some of you face this problem, an alternative is: Don't disable the whole logging subsystem, instead just exclude slf4j libraries provided by JBoss/Wildfly, to use the one used by spring-boot.
<jboss-deployment-structure>
<deployment>
<exclusions>
<module name='org.slf4j'/>
<module name='org.slf4j.impl'/>
</exclusions>
</deployment>
</jboss-deployment-structure>
Hope helps somebody.
I am using my own logging configuration, log4j2 xml instead of spring's logging and faced the same issue with the Wildfly. I commented the whole subsytem system for over-riding that.

Override logging in WildFly

I used tomcat and simply override default logging system. How to enable logging with logback on wildfly in my spring app?
My Logback.xml owrking on tomcat
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="WARN" />
<logger name="com.citronium.planstery" level="INFO" />
<logger name="org.apache.http.impl.conn.tsccm" level="ERROR" />
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
You can use logback to configure logging in your application. You can't use logback to configure logging for the server.
To use logback in your configuration you'll need to change the add-logging-api-dependencies to false or create a jboss-deployment-structure.xml that excludes the subsystem. You'll also need to include logback and slf4j in your deployment.
The first option of changing the add-logging-api-dependencies is a global setting for all deployments. The follow CLI command will change the value:
/subsystem=logging:write-attribute(name=add-logging-api-dependencies,value=false)
This option simply doesn't add any of the implicit logging dependencies to your deployment.
The second option of using a jboss-deployment-structure.xml will disable the logging subsystem for your deployment only. The following is an example file:
<jboss-deployment-structure>
<deployment>
<!-- exclude-subsystem prevents a subsystems deployment unit processors running on a deployment -->
<!-- which gives basically the same effect as removing the subsystem, but it only affects single deployment -->
<exclude-subsystems>
<subsystem name="logging" />
</exclude-subsystems>
</deployment>
</jboss-deployment-structure>
Here is how we do this, by the way, we are using wildfly-8.1.0-Final.
First, make a jar file containing this class:
https://gist.github.com/xiaodong-xie/219491e0b433f8bd451e
Then put this jar file into "wildfly-8.1.0.Final/modules/system/layers/base/org/jboss/logmanager/main", and add a reference to this jar file in the module.xml file in the exact same folder.
Then put "logback-classic-1.1.2.jar" and "logback-core-1.1.2.jar" (You can use any version of logback you choose) into "wildfly-8.1.0.Final/modules/system/layers/base/org/jboss/logmanager/main", and reference those 2 jar files in the module.xml file.
Add the following to the "subsystem:logging" in the standalone.xml you are using:
<custom-handler name="logback" class="org.slf4j.bridge.SLF4JBridgeHandler" module="org.jboss.logmanager"></custom-handler>
And reference this handler in the root-logger element following:
<root-logger>
<level name="INFO"/>
<handlers>
<handler name="CONSOLE"/>
<handler name="logback"/>
</handlers>
</root-logger>
Here is an example of logback.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator"/>
<appender name="LOGFILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${JBOSS_HOME}/standalone/log/server-logback.log</file>
<append>true</append>
</appender>
<appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="LOGFILE"/>
</appender>
<root level="INFO">
<appender-ref ref="ASYNC"/>
</root>
</configuration>
And put this logback.xml file into "wildfly-8.1.0.Final/standalone/configuration" folder.
Add the following to the "standalone.sh" or equivalent in the "wildfly-8.1.0.Final/bin" folder.
-Dlogback.configurationFile=file:$JBOSS_CONFIG_DIR/logback.xml
Just under "-Dlogging.configuration=file:$JBOSS_CONFIG_DIR/logging.properties" line. There are 2 places in "standalone.sh" file.
=================================================================================
Or you can do it in a simpler way. :)
Put the 2 logback jar files to the "jboss.logmanager" module, and add "-Dorg.jboss.logging.provider=slf4j" to the "standalone.sh" file, the same position.
I found there are some logging missing if going this by the way, since Wildfly itself still using its own logging facility if going this way.
Have fun. :-)

Use my log4j under jboss 6

I want to deploy my web application on JBOSS6. The applicaation itself works, but the logging doens't. I use log4j and have added a jboss-deployment-structure.xml to my war. The contents are
<jboss-deployment-structure>
<deployment>
<!-- Exclusions allow you to prevent the server from automatically adding some dependencies -->
<exclusions>
<module name="org.apache.log4j" />
<module name="org.jboss.logging" />
</exclusions>
</deployment>
In my log4j.xml I have
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "dtd/log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender name="LogAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C:\\logs\\web.log"/>
<param name="MaxFileSize" value="10000KB"/>
<param name="MaxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%x %-5p [%d{yyyyMMdd HH:mm:ss}] - %c:%L - %m%n"/>
</layout>
</appender>
<logger name="be.sofico.web">
<level value="debug" />
<appender-ref ref="LogAppender" />
</logger>
This all works fine on tomcat and websphere (when I set classloading parent last)
How can I get it to work on JBOSS 6?
I solved my problem doing the following:
put jboss-deployment-structure.xml inside web\META-INF with the following in the file
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
<deployment>
<exclusions>
<module name="org.apache.log4j" />
<module name="org.apache.commons.logging" />
</exclusions>
</deployment>
</jboss-deployment-structure>
And add this to the startup of the server: -Dorg.jboss.as.logging.per-deployment=false
to set my log4j in the classpath correctly i did 2 things :
1) I set the name of the log4j to use like this
-Dlog4j.configuration=fox-log4j.xml
this one has to be in the CLASSPATH
2) I call the logging manager explicitly otherwise the jboss log4j wont work
this gives in my run.conf :
#parameter used by the JVM and call later in the log4j.xml
LOG_FOLDER=$DIRNAME/../server/default/log
#jvm options
JAVA_OPTS="-Xms256m -Xmx4096m -XX:MaxPermSize=1024m -Dlog4j.configuration=fox-log4j.xml \
-Dfile.encoding=UTF-8 -Dfile.io.encoding=UTF-8 -DjavaEncoding=UTF-8 -Djboss.platform.mbeanserver \
-Djavax.management.builder.initial=org.jboss.system.server.jmx.MBeanServerBuilderImpl \
-Djava.util.logging.manager=org.jboss.logmanager.LogManager \
-Dorg.jboss.logging.Logger.pluginClass=org.jboss.logging.logmanager.LoggerPluginImpl \
-DLOG_FOLDER=$LOG_FOLDER"
now a piece of my log4j :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender name="FOX_LOG" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="DEBUG"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="25MB"/>
<param name="MaxBackupIndex" value="5"/>
<param name="File" value="${LOG_FOLDER}/fox.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-15x %t %-5p %-50.50c{1} - %m%n"/>
</layout>
</appender>
<category name="com.mycompany" additivity="true">
<priority value="DEBUG"/>
<appender-ref ref="FOX_LOG"/>
</category>
<root>
<priority value="INFO"/>
<appender-ref ref="FILE"/>
</root>
</log4j:configuration>
hope this could help.
regards
It means it is not loading your log4j.xml. Something wrong with the xml file location or issue with the class loader finding this log4j.xml.