How to output environment variable to Grails log4j log file - grails

I have the following file appender and I'm wanting the specific environment to be output to the log file whenever a message is logged:
appenders {
rollingFile name:'mtagradepush_file',
maxFileSize: 2048,
file: "${globalDirs.logDirectory}${appName}.log".toString(),
layout:pattern(conversionPattern: "[Env:${app.log.env}] %d{yyyy-MM-dd HH:mm:ss} %5p %c{1}:%L - %m%n")
}
This conversion pattern worked when I used it in a log4j.properties file in a normal Java app, but now when using it with Grails it's not printing the value of the environment to the log file.
The app.log.env variable is being set in CATALINA_OPTS when the Tomcat 6 server starts up, like this:
export CATALINA_OPTS="-Xms128m -Xmx2000m -XX:MaxPermSize=512m
-Dapp.log.env=DEVL..."
If it's not possible to reference this variable then is there a way to grab the Grails environment variable?

Treat the ${} as a block of Groovy code and do a ${System.getProperty('app.log.env')}

Related

Why does log4j work correctly on windows and Linux, the same configuration not working in docker?

I use tomcat as server and log4j can work on windows and Linux. When I deploy it with Docker, log4j can't work normally. And I find nothing is created in related folder. I don't find any error in catalina.log. I check the access rights for tmp folder and writing is allowed. Do you have any idea about source of the problem? Thanks.
log4j.rootLogger=INFO, DailyRolling, Terminal, CONSOLE
#log4j.rootLogger=DEBUG, A1, A2, DailyRolling, Terminal, CONSOLE
log4j.appender.DailyRolling=org.apache.log4j.RollingFileAppender
log4j.appender.DailyRolling.layout=org.apache.log4j.PatternLayout
log4j.appender.DailyRolling.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-7p (%c) %m%n
log4j.appender.DailyRolling.DatePattern='.'yyyy-MM-dd-a
#LINUX
log4j.appender.DailyRolling.File=/tmp/RolingLog.log
log4j.appender.DailyRolling.DatePattern=yyyy-MM-dd'.log'
log4j.appender.DailyRolling.Threshold=INFO
log4j.appender.DailyRolling.BufferedIO=true
log4j.appender.DailyRolling.BufferSize=8192
log4j.appender.DailyRolling.MaxFileSize=20MB
log4j.appender.DailyRolling.MaxBackupIndex=100
log4j.appender.DailyRolling.MaxBackupDay=10
log4j.appender.Terminal=org.apache.log4j.RollingFileAppender
log4j.appender.Terminal.MaxBackupDay=10
#LINUX
log4j.appender.Terminal.File=/tmp/LogConsole.log
log4j.appender.Terminal.layout=org.apache.log4j.PatternLayout
log4j.appender.Terminal.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-7p (%c) %m%n
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
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
log4j.logger.httpclient.wire=INFO
log4j.logger.org.apache.commons=INFO
log4j.logger.org.hibernate=WARN
log4j.logger.org.oracle=INFO
log4j.logger.com.mchange.v2=INFO
log4j.logger.org.apache.commons.httpclient=WARN
log4j.logger.org.apache.xerces=ERROR
log4j.logger.org.springframework=INFO

How to view Tomcat logs in PCFDev?

I'm deploying a 2.3.5 Grails application with mysql in PCFDev. Hitting some URLs in my application I get a 500 error. When I view the logs with cf logs my-sample I only see access logs. They don't show my stacktrace.
How can I actually see the stacktrace in the logs so that I know exactly what is causing the error? Usually when deploying the app in Tomcat, I would see these logs in /logs directory of Tomcat.
This is what my config.groovy looks like for my grails application.
def catalinaBase = System.properties.getProperty('catalina.base')
if (!catalinaBase) catalinaBase = '.' // just in case
def logDirectory = "${catalinaBase}/logs"
log4j = { root ->
appenders {
// console name: 'stdout', layout: pattern(conversionPattern: "%d [%t] %-5p %c %x - %m%n")
rollingFile name:'stdout', file:"${logDirectory}/my.log".toString(), maxFileSize:'100KB'
rollingFile name:'stacktrace', file:"${logDirectory}/my_stack.log".toString(), maxFileSize:'100KB'
}
warn 'org.codehaus.groovy.grails.web.servlet', // controllers
'org.codehaus.groovy.grails.web.pages', // GSP
'org.codehaus.groovy.grails.web.sitemesh', // layouts
'org.codehaus.groovy.grails.web.mapping.filter', // URL mapping
'org.codehaus.groovy.grails.web.mapping', // URL mapping
'org.codehaus.groovy.grails.commons', // core / classloading
'org.codehaus.groovy.grails.plugins', // plugins
'org.codehaus.groovy.grails.orm.hibernate', // hibernate integration
'org.springframework',
'org.hibernate'
debug 'com.aerstone.ldap', 'com.aerstone.scanner.helper'
root.level = org.apache.log4j.Level.INFO
}
Googling "cf logs" brings up http://docs.pivotal.io/pivotalcf/1-8/devguide/deploy-apps/streaming-logs.html as the first result. It has a section on "Writing to the Log from Your App". It says:
Your app must write logs to STDERR or STDOUT. Both are typically buffered, and you should flush the buffer before delivering the message to Loggregator.
In addition to writing to a directory or file, you should consider also sending that output to STDOUT and STDERR streams as per the Twelve-Factor App treatment of logs.
Also, you can search "cf ssh" which gives this result explaining how you can SSH into your apps' containers and look at things on their local file system.

[Jboss 4.3][Grails 2.3.x] Log does not work properly (ERROR: invalid console appender config detected, console stream is looping)

If we deploy our Grails application in a JBoss 4.3, it always shows the same error message:
ERROR: invalid console appender config detected, console stream looping
In the Config.groovy file we have defined the log4j:
log4j = {
root {
info 'unoAppender','communications', 'hibernate', 'dependencies'
}
appenders {
appender new CustodianDailyRollingFileAppender(
name:'unoAppender',
file:'logs/app-uno.log',
append:true, maxNumberOfDays:60,
compressBackups:true,
threshold:Level.DEBUG,
layout: pattern(conversionPattern: "%d{ISO8601} %-5p [%t:%1X{JSESSION_ID}] [%c{1}] - %m%n"))
.....
}
}
And also there are appenders in the jboss-log4j.xml of the default server.
Finally we have tried to add the following paramenter in the JAVA_OPTS of the run.sh [-Dorg.jboss.logging.Log4jService.catchSystemOut=false] the logs works properly in the console but it does not work in the server.log.

Grails javamelody plugin warnings

I've been using the javamelody monitoring plugin for a while in Grails with no problem, but lately I had to move my developments to another computer (I'm now using netbeans 7.1.2). After reinstalling the plugins, I run the app flawlessly in my development environment. But when I run the war to my production environment, the following warnings show up:
log4j:WARN No appenders could be found for logger (net.bull.javamelody).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
The appenders section in Config.groovy is:
appenders {
console name:'stdout', layout:pattern(conversionPattern: '%d [%t] %-5p %c{2} %x - %m%n')
appender new DailyRollingFileAppender (
name: 'dailyAppender',
datePattern: "'.'yyyy-MM-dd",
fileName: "logs/${appName}.log",
layout: pattern(conversionPattern:'%d [%t] %-5p %c{2} %x - %m%n')
)
}
My questions are:
Why aren't these warnings present in the development environment's log ?What are possible consecuences on the application ?
I'm running Grails 1.3.9, Melody 1.2, tomcat 7.0.23
Thanks
It might be different form environment to environment if you have
setup a logging appender in the development section of your Grails
Config.groovy file but not for your production section.
The consequences are that you will not have logging. You may have code in your application that says log.error("Critical Error!") but since this is not linked to any appender you will never see it anywhere.
Check out logging in the documentation.

Where can I find the Tomcat log written out by 'grails run-app'?

While running grails app in 'dev' mode using 'grails run-app', where is the default Tomcat log file located written out by the embedded Tomcat come with Grails (1.2.2) installation?
There isn't a default log file, the output to the log gets written to stdout.
Should be $CATALINA_HOME/logs/catalina.out
You have to define an log4j root logger in your Config.groovy like this:
log4j = {
appenders {
// console name:'stdout', layout:pattern(conversionPattern: '%c{2} %m%n')
// file name:'file', file:'app.log'
}
// By default, messages are logged at the warn level to the console and the app.log
root {
warn 'stdout'
// warn 'stdout','file'
additivity = true
}
...
}
This example also shows how to configure the logging pattern. Also it shows how to configure file logging. The appenders section is optional and just needed to configure the logging pattern or file logger.

Resources