I'm trying to get started Log4j in Spring MVC application, but I'm unable to get information, what's wrong. Each blog post is same: It's really easy. Just put log4j.properties into /WEB-INF/classes directory. But for me it does not work. The problem is, that there is no place to look for error message. The only I know is, that expected log file was not created. Is there some possibility to debug it? Really to put log4j.properties file in /WEB-INF/classes is enought?
The above mentioned log4j.properties file follows:
#Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:\\workspace-trainee-actual\\0pokusy\\Sprung\\logik.txt
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# Root logger option
log4j.rootLogger=trace, file
Controller using Log4j:
#Controller
public class HelloWorldController {
private Logger log = Logger.getRootLogger();
#RequestMapping("/")
public ModelAndView base() {
log.debug("base URI");
ModelAndView mv = new ModelAndView();
mv.setViewName("index");
return mv;
}
}
The only sure fact is, that it work's, so log is not null and the Log4j library is available.
Try adding
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
to your web.xml file
I have changed logging threshold of Geronimo to debug and I found '2011-06-24 07:33:18,353 DEBUG [root] base URI'. I thing there is some conflict. The application is one process, one JVM instance with thousands of classes, but with only one root logger for all of them.
Seams that you are right:
From the Geronimo Documentation:
Note that in any case, unless you use hidden-classes or inverse-classloading to load your own copy of log4j separate from the geronimo copy, log4j will not automatically read any log4j.properties files you may have included in your classpath.
I found the last "solution" from the documentation very interesting:
Copy the log4j.properties file by hand to the appropriate location such as var/my-app/log4j.properties. There is no need to include this file in your app.
Because that allows you to externalize the log4j configuration. So the Operations-Guy can manage and change the log4j configuration. And you do not need to build/and deploy a new version if for example the directory where the files are stored is changed.
Workaround
The system works properly allways. The Log4j system is configured for our instance of the Java Virtual Machine. Geronimo has already done it. We can not reconfigure the root logger, but we can use it. The default threshold is INFO and application uses root logger for a debug message. Thus we cannot see it anywhere.
If threshold has decreased to DEBUG, the message appears in Geronimo log. I have changed in the file $GERONIMO_HOME/var/log/server-log4j.properties a line at the beginning: log4j.rootLogger=DEBUG, CONSOLE, FILE And in $GERONIMO_HOME/var/log/geronimo.log I can then read: 2011-06-24 20:02:21,375 DEBUG [root] base URI
From some unknown reason is neither under Linux nor under Windows created separated output file. We can the message find just in server log, but it does not matter, we can overcome it. Let rename the logger in Log4j configuration: #Root logger for application
log4j.logger.springTestLogger=TRACE, APLOK
And in the code accordingly: private Logger log = Logger.getLogger("springTestLogger");
We create the separete log file under Linux easily: cat $GERONIMO_HOME/var/log/geronimo.log|grep springTestLogger > separe.log
Try with
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/properties/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
Related
I have a spring boot application bundled as war file , and able to push to App Engine
But I am getting problems starting app (I suspect there could be an issue with DB too...but couldnt remember where I saw...a nightmare)
java.lang.RuntimeException: java.lang.IllegalStateException: Logback
configuration error detected: ERROR in
ch.qos.logback.core.rolling.RollingFileAppender[FILE] - Failed to
create parent directories for
[/base/data/home/apps/e~pemy/20210716t001812.436581063072799646/logs/pynew.log]
ERROR in ch.qos.logback.core.rolling.RollingFileAppender[FILE] -
openFile(logs/pynew.log,true) call failed.
java.io.FileNotFoundException: logs/pynew.log (No such file or
directory)
I am using the below properties in my application props
> logging.file.path=logs
> logging.file.name=${logging.file.path}/pynew.log
I am finding it very hard to include google specific dependencies and properties , and making a mess of my project...created app.yaml, web-inf>> appengine-web xml, logging.properties (not sure why but added as told in a tutorial)
Question: How can I create parent directory or link to cloud storage folder etc?
I also want to specify a profile and I see I can do it in yaml file. Is this used only
env_variables:
JAVA_USER_OPTS: '-Dspring.profiles.active=prod'
But I would like to know how to connect to Cloud SQL
spring.datasource.url=jdbc:mysql:///mydb?cloudSqlInstance=myapp:europe-west2:dBinstancename&socketFactory=com.google.cloud.sql.mysql.SocketFactory
spring.datasource.username=${dbuser}
spring.datasource.password=ENC(${dbencpwd})
spring.cloud.gcp.sql.database-name=mydb
spring.cloud.gcp.sql.instance-connection-name=myapp:europe-west2:dBinstancename
It is so confusing that I keep forgetting which connection needs password and which wont. and keep breaking my local
Question
Assuming that I need to supply credentials, How can I supply - ${dbuser}
I used the default spring logger with logback-spring.xml for all my development, and this is not working on AppEngine
So I followed https://cloud.google.com/logging/docs/setup/java
and added logback.xml and the dependency
implementation 'com.google.cloud:google-cloud-logging-logback:0.121.3-alpha'
The essence of the problem: how to transfer your own web.xml file to
new org.apache.tomee.embedded.Configuration().setWebXml("file_name").
When I set http port as new org.apache.tomee.embedded.Configuration().setHttpPort(_port) works fine.
But, when set web.xml it does not work.
I think setWebXml is a dead config but you can setConf("/folder/in/resources") and put a web.xml in this folder. It will put it in conf folder and do what you want
Just made my first NAR from a tutorial, put it in the /lib file and restarted NiFi. The Processor shows up as loaded in the logs but does not show up in the processor list. Any ideas?
Tutorial: http://www.nifi.rocks/developing-a-custom-apache-nifi-processor-json/
#SideEffectFree
#Tags({"JSON", "NIFI ROCKS"})
#CapabilityDescription("Fetch value from json path.")
public class JsonProcessor extends AbstractProcessor {
...
}
Edit:
I see two lines in the logs with this in it:
2017-07-06 19:34:06,200 INFO [main] org.apache.nifi.nar.NarClassLoaders Loaded NAR file: C:\UserApps\NIFI-1~1.0\.\work\nar\extensions\examples-1.0-SNAPSHOT.nar-unpacked as class loader org.apache.nifi.nar.NarClassLoader[.\work\nar\extensions\examples-1.0-SNAPSHOT.nar-unpacked]
What should the processor be called in the list? JsonProcessor?
Edit:
Tried changing the <nifi.version>1.3.0</nifi.version> in the pom from 1.2.0 to 1.3.0. No joy.
So on top of coding up the program you must put a file called
org.apache.nifi.processor.Processor
in YOUR project to get it to show up. I would suspect NiFi would scan for the classes with the annotations but alas, it does not. Probably some security thing.
The tutorial above does have this in it. "Browsing" got me again!
I also used this tutorial to create a Customer Processor. The issue I had was that I mixed up the ./lib directory.
When you start Apache Nifi, you would notice the following at the beginning of the log:
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home
NiFi home: /usr/local/Cellar/nifi/1.6.0/libexec
Confirm that the NAR file is the $NIFI_HOME/libexec/lib folder which would be /usr/local/Cellar/nifi/1.6.0/libexec/lib in my case.
I hope this helps
NB: I ran Apache Nifi on a Mac, logs might be different on another OS
I need to keep separate log4j log files in a separate folder.Those log files should be separated for each URL.My project is Spring MVC project.
For example,
www.xyz.com/test/url1
www.xyz.com/test/url2
www.xyz.com/test/url3
How can I configure my log4j?
Is there a way to keep separate log4j files for method level?
Its possible. Yo need to create different logger instances in a class say logger, logger1, logger2 and these should point to different files. Although they will use the same base package to cover but you can move logs in different files for different methods
Here I am posting sample code for configuring multiple loggers.
I have configured my log4j file in this way.
log4j.rootLogger = INFO , stdout
#log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#log4j.appender.stdout.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n
log4j.appender.pageOneLog=org.apache.log4j.FileAppender
log4j.appender.pageOneLog.File=C:/work/logs/pageOneLog.log
log4j.appender.pageOneLog.layout=org.apache.log4j.PatternLayout
log4j.appender.pageOneLog.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n
log4j.appender.pageTwoLog=org.apache.log4j.FileAppender
log4j.appender.pageTwoLog.File=C:/work/logs/pageTwoLog.log
log4j.appender.pageTwoLog.layout=org.apache.log4j.PatternLayout
log4j.appender.pageTwoLog.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n
log4j.category.pageOneLogger=INFO, pageOneLog
log4j.additivity.pageOneLogger=false
log4j.category.pageTwoLogger=INFO, pageTwoLog
log4j.additivity.pageTwoLogger=false
Then use this loggers in the Java code following manner.
Logger log1 = Logger.getLogger("pageOneLogger");
Logger log2 = Logger.getLogger("pageTwoLogger");
How do I load ${catalina.home}/conf/application.properties in a Spring / Tomcat webapp?
Looking around on StackOverflow and Google I see many discussions which claim it's possible. However, it's just not working for me. In line with the advice from my research my Spring applicationContext.xml file contains the following line:
<context:property-placeholder location="${catalina.home}/conf/application.properties"/>
But I get this in the logs:
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/Users/username/Servers/apache-tomcat-6.0.36/conf/application.properties]
From the log entry I can see that ${catalina.home} is expanding correctly. When I expand it by hand in the applicationContext.xml file it returns the same error. The following returns the contents of the application.properties file as expected:
cat /Users/username/Servers/apache-tomcat-6.0.36/conf/application.properties
So the path is clearly correct. Is this a webapp security or Tomcat server configuration issue?
The location of a context:property-placeholder is a Resource, which means that if you provide just a file path (as opposed to a full URL with a protocol) then the path will be resolved against the base directory of the webapp - it is trying to load /Users/username/Servers/apache-tomcat-6.0.36/webapps/<appname>/Users/username/Servers/apache-tomcat-6.0.36/conf/application.properties, which does not exist. If you prefix it with file: it'll work as you require:
<context:property-placeholder location="file:${catalina.home}/conf/application.properties"/>
For annotation based configuration you can use:
#PropertySource("file:${catalina.home}/conf/application.properties")