Logging errors in a grails application in the logs - exception

When my Grails application crashes, it shows the error and the stacktrace on the error page because the error.gsp page has the following snippet <g:renderException exception="${exception}" />. However nothing gets logged in the log file.
How can I change this? because for the production application I plan to remove the renderException because I don't want users to see the entire stacktrace.
My log4j settings are as follows:
appenders {
rollingFile name:'catalinaOut', maxFileSize:1024, fileName:"${System.properties.getProperty('catalina.home')}/logs/mylog.log"
}
root {
error 'catalinaOut'
debug 'catalinaOut'
additivity = true
}
error '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',
'net.sf.ehcache.hibernate',
'grails.app'
debug 'grails.app'
}
I'm running the app in development as grails run-app

I use these settings for console and file based logging. You can remove stdout if you don't want/need console. Just copy all your error classes in the corresponding list.
log4j = {
def loggerPattern = '%d %-5p >> %m%n'
def errorClasses = [] // add more classes if needed
def infoClasses = ['grails.app.controllers.myController'] // add more classes if needed
def debugClasses = [] // add more classes if needed
appenders {
console name:'stdout', layout:pattern(conversionPattern: loggerPattern)
rollingFile name: "file", maxFileSize: 1024, file: "./tmp/logs/logger.log", layout:pattern(conversionPattern: loggerPattern)
}
error stdout: errorClasses, file: errorClasses
info stdout: infoClasses, file: infoClasses
debug stdout: debugClasses, file: debugClasses
}

Related

How to run Karate and Gatling with Gradle build system for performance testing

I'm trying to run a Karate test as a Gatling performance test. I am being use Gradle however. When trying to run under Gradle the below disaster unfolds.
Appreciate any ideas what might be causing KarateAction to crash.
i have added Gatling dependencies in gradle build
BUILD GRADLE:
plugins {
// id 'org.springframework.boot' version '2.5.5'
// id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'net.researchgate.release' version '2.8.1'
}
group 'org.ikano'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
ext {
cucumberVersion = "5.7.0"
karateVersion = '1.1.0'
// kafkaProducer = "3.2.0"
// kafkaConsumer = "3.2.0"
// kafkaCommonModel = "3.2.0"
dbUtilsVersion = "1.7"
}
apply plugin: 'scala'
configurations {
gatling
}
sourceSets {
test {
resources {
srcDir file('src/test/java')
exclude '**/*.java'
exclude '**/*.scala'
}
}
}
test {
useJUnitPlatform()
systemProperty "karate.options", System.properties.getProperty("karate.options")
systemProperty "karate.env", System.properties.getProperty("karate.env")
outputs.upToDateWhen { false }
}
repositories {
maven {
url "https://artifacts.le.tnt.bank.*****/repository/maven-****"
credentials {
username '*******'
password '**********'
}
mavenCentral()
}
}
task karateDebug(type: JavaExec) {
classpath = sourceSets.test.runtimeClasspath
main = 'com.intuit.karate.cli.Main'
}
dependencies {
implementation("commons-configuration:commons-configuration:1.10")
implementation("org.json:json:20180813")
implementation("javax.mail:mail:1.5.0-b01")
implementation("com.jayway.jsonpath:json-path:2.6.0")
implementation("com.googlecode.json-simple:json-simple:1.1.1")
// implementation("org.springframework:spring-context:5.3.13")
implementation("org.apache.poi:poi-ooxml:4.1.1")
// implementation("org.springframework:spring-webmvc:4.3.0.RELEASE")
testImplementation("com.intuit.karate:karate-core:1.2.0")
testImplementation("junit:junit:4.13.2")
testImplementation("com.intuit.karate:karate-junit5:1.1.0")
testImplementation("com.intuit.karate:karate-gatling:1.1.0")
testImplementation("com.intuit.karate:karate-apache:0.9.6")
testImplementation("net.masterthought:cucumber-reporting:3.8.0")
testImplementation("hu.blackbelt.bundles.poi:org.apache.poi:4.1.2_2")
testImplementation("org.apache.poi:poi:4.1.1")
testImplementation("io.rest-assured:rest-assured:4.3.0")
testCompile 'com.intuit.karate:karate-junit5:1.1.0'
testCompile "com.intuit.karate:karate-gatling:${karateVersion}"
// compile group: 'org.springframework.boot', name: 'spring-boot-starter-parent', version: '2.5.5', ext: 'pom'
//implementation group: 'org.springframework', name: 'spring-websocket', version: '5.2.9.RELEASE'
//implementation group: 'org.springframework', name: 'spring-messaging', version: '5.2.9.RELEASE'
// implementation 'org.springframework.boot:spring-boot-starter-web'
// implementation 'org.springframework.boot:spring-boot-starter'
// testImplementation('org.springframework.boot:spring-boot-starter-test')
//{exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'}
compile group: 'joda-time', name: 'joda-time', version: '2.10.6'
// implementation "com.ikano.ipay.eem.kafka:kafka-producer:${kafkaProducer}"
// implementation "com.ikano.ipay.eem.kafka:kafka-consumer:${kafkaConsumer}"
// implementation "com.ikano.ipay.eem.kafka:kafka-common-model:${kafkaCommonModel}"
implementation group: 'redis.clients', name: 'jedis', version: '2.9.0'
implementation group: 'org.redisson', name: 'redisson', version: "3.16.3"
compile group: 'org.apache.poi', name: 'poi', version: '4.1.2'
compile group: 'org.apache.poi', name: 'poi-ooxml', version: '4.1.2'
compile group: 'commons-dbutils', name: 'commons-dbutils', version: "$dbUtilsVersion"
task gatlingRun(type: JavaExec) {
group = 'Web Tests'
description = 'Run Gatling Tests'
new File("${buildDir}/reports/gatling").mkdirs()
classpath = sourceSets.test.runtimeClasspath
main = "io.gatling.app.Gatling"
args = [
// change this to suit your simulation entry-point
'-s', 'performance2.userSimulation',
'-rf', "${buildDir}/reports/gatling"
]
systemProperties System.properties
}
group = "org.ikano"
version = "1.0-SNAPSHOT"
description = "IPay"
java.sourceCompatibility = JavaVersion.VERSION_1_8
FOR SCALA
package performance
import com.intuit.karate.gatling.PreDef._
import io.gatling.core.Predef._
import io.gatling.core.structure.ScenarioBuilder
import scala.concurrent.duration._
class UserSimulation extends Simulation{
val getSingleUser: ScenarioBuilder = scenario("UK Onboarding - Verify user is able to onboard successfully").exec(karateFeature("classpath:performance/onboarding.feature"))
setUp(
getSingleUser.inject(rampUsers(10).during(5 seconds))
)
}
Command Line :gradle gatlingRun
Starting a Gradle Daemon (subsequent builds will be faster)
> Task :gatlingRun
16:10:18.039 [main] INFO i.g.c.config.GatlingConfiguration$ - Gatling will try to use 'gatling.conf' as config file.
16:10:20.052 [GatlingSystem-akka.actor.default-dispatcher-6] INFO akka.event.slf4j.Slf4jLogger - Slf4jLogger started
16:10:22.818 [main] ERROR io.gatling.app.Gatling$ - Run crashed
java.lang.IllegalArgumentException: User defined Simulation class performance2.userSimulation could not be loaded
at io.gatling.app.Selection$Selector.findUserDefinedSimulationInClassloader$1(Selection.scala:80)
at io.gatling.app.Selection$Selector.$anonfun$singleSimulationFromConfig$4(Selection.scala:85)
at scala.Option.orElse(Option.scala:477)
at io.gatling.app.Selection$Selector.$anonfun$singleSimulationFromConfig$3(Selection.scala:85)
at scala.Option.flatMap(Option.scala:283)
at io.gatling.app.Selection$Selector.singleSimulationFromConfig(Selection.scala:83)
at io.gatling.app.Selection$Selector.$anonfun$selection$1(Selection.scala:52)
at scala.Option.getOrElse(Option.scala:201)
at io.gatling.app.Selection$Selector.selection(Selection.scala:44)
at io.gatling.app.Selection$.apply(Selection.scala:36)
at io.gatling.app.Runner.run0(Runner.scala:61)
at io.gatling.app.Runner.run(Runner.scala:49)
at io.gatling.app.Gatling$.start(Gatling.scala:83)
at io.gatling.app.Gatling$.fromArgs(Gatling.scala:45)
at io.gatling.app.Gatling$.main(Gatling.scala:37)
at io.gatling.app.Gatling.main(Gatling.scala)
Caused by: java.lang.ClassNotFoundException: performance2.userSimulation
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at io.gatling.app.Selection$Selector.$anonfun$singleSimulationFromConfig$2(Selection.scala:71)
at scala.util.Try$.apply(Try.scala:210)
at io.gatling.app.Selection$Selector.findUserDefinedSimulationInClassloader$1(Selection.scala:71)
... 15 common frames omitted
Exception in thread "main" java.lang.IllegalArgumentException: User defined Simulation class performance2.userSimulation could not be loaded
at io.gatling.app.Selection$Selector.findUserDefinedSimulationInClassloader$1(Selection.scala:80)
at io.gatling.app.Selection$Selector.$anonfun$singleSimulationFromConfig$4(Selection.scala:85)
at scala.Option.orElse(Option.scala:477)
at io.gatling.app.Selection$Selector.$anonfun$singleSimulationFromConfig$3(Selection.scala:85)
at scala.Option.flatMap(Option.scala:283)
at io.gatling.app.Selection$Selector.singleSimulationFromConfig(Selection.scala:83)
at io.gatling.app.Selection$Selector.$anonfun$selection$1(Selection.scala:52)
at scala.Option.getOrElse(Option.scala:201)
at io.gatling.app.Selection$Selector.selection(Selection.scala:44)
at io.gatling.app.Selection$.apply(Selection.scala:36)
at io.gatling.app.Runner.run0(Runner.scala:61)
at io.gatling.app.Runner.run(Runner.scala:49)
at io.gatling.app.Gatling$.start(Gatling.scala:83)
at io.gatling.app.Gatling$.fromArgs(Gatling.scala:45)
at io.gatling.app.Gatling$.main(Gatling.scala:37)
at io.gatling.app.Gatling.main(Gatling.scala)
Caused by: java.lang.ClassNotFoundException: performance2.userSimulation
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at io.gatling.app.Selection$Selector.$anonfun$singleSimulationFromConfig$2(Selection.scala:71)
at scala.util.Try$.apply(Try.scala:210)
at io.gatling.app.Selection$Selector.findUserDefinedSimulationInClassloader$1(Selection.scala:71)
... 15 more
> Task :gatlingRun FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':gatlingRun'.
> Process 'command 'C:\Program Files\Amazon Corretto\jdk1.8.0_282\bin\java.exe'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.9.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD FAILED in 44s
3 actionable tasks: 1 executed, 2 up-to-date
The package name is performance but inside the log it says:
performance2.userSimulation
Can you change it to in the run command:
performance.userSimulation
I think that is the issue you are facing. It cant find the simulation class.

GRAILS-2.5 Handling syntax errors on external configuration

I have application developed using Grails 2.5.
In the the "Config.groovy" file i have included external configuration file like this:
grails.config.locations = []
def locationAdder = ConfigFinder.&addLocation.curry(grails.config.locations)
[CONFIG-1 : "base_config.groovy",
CONFIG-2 : "app_configuration.groovy"
].each { envName, defaultFileName -> locationAdder(envName, defaultFileName) }
In the "app_configuration.groovy" file i have all the application level configuration.
My question is how to catch the "syntax errors" when server is loading this configuration files, like ex.:
if i have configuration like
some_configuration=["key": "value"]
and if it has an syntax errors like
some_configuration=["key": "value
Notice that above it missed double quote and ending bracket, in this case the server will not load all the configurations.
If any one know that how to catch exception and reload the configurations with corrected configuration.
You can not catch Exception in external config. You may just add some log which in case of failure, at least have got some clue where it is failed.
println "External config: Part 1 loaded "
println "External Config: Part n loaded "
....

How do I configure jooq with Gradle and Mysql for a single database

I'm trying to use jooq to load configurations automatically from gradle but had a hard time following the guide.
I finally have it loading data, but so far I can only get all databases to work (by having the database() chunk be blank).
My code below has my attempt to load only one database.
buildscript {
repositories {
mavenCentral()
maven {
name 'JFrog OSS snapshot repo'
url 'https://oss.jfrog.org/oss-snapshot-local/'
}
jcenter()
}
dependencies {
classpath 'org.jooq:jooq-codegen:3.9.1'
classpath group: 'mysql', name: 'mysql-connector-java', version: '6.0.6'
}
}
apply plugin: 'application'
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'antlr'
repositories {
mavenLocal()
mavenCentral()
jcenter()
}
dependencies {
//compile group: 'com.github.javaparser', name: 'javaparser-core', version: '3.0.0-alpha.2'
compile group: 'com.github.javaparser', name: 'java-symbol-solver-core', version: '0.5.2'
compile 'org.jooq:jooq:3.9.1'
runtime group: 'mysql', name: 'mysql-connector-java', version: '6.0.6'
testCompile "junit:junit:latest.release"
}
idea {
module {
excludeDirs += file('src/main/resources')
}
}
// Use your favourite XML builder to construct the code generation configuration file
// ----------------------------------------------------------------------------------
def writer = new StringWriter()
def xml = new groovy.xml.MarkupBuilder(writer)
.configuration('xmlns': 'http://www.jooq.org/xsd/jooq-codegen-3.9.0.xsd') {
jdbc() {
driver('com.mysql.cj.jdbc.Driver')
url('jdbc:mysql://127.0.0.1/graphUpgrade?serverTimezone=UTC')
user('parseUser')
password('password')
}
generator() {
database() {
name('org.jooq.util.mysql.MySQLDatabase')
inputSchema('graphUpgrade')
includes('.*')
}
// Watch out for this caveat when using MarkupBuilder with "reserved names"
// - https://github.com/jOOQ/jOOQ/issues/4797
// - http://stackoverflow.com/a/11389034/521799
// - https://groups.google.com/forum/#!topic/jooq-user/wi4S9rRxk4A
generate([:]) {
pojos true
daos true
}
target() {
packageName('us.klingman.codeParser.db')
directory('src/main/java')
}
}
}
print writer.toString()
// Run the code generator
// ----------------------
org.jooq.util.GenerationTool.generate(
javax.xml.bind.JAXB.unmarshal(new StringReader(writer.toString()), org.jooq.util.jaxb.Configuration.class)
)
Running this code produces the following error:
Error while fetching tables
java.lang.NullPointerException
at org.jooq.util.AbstractElementContainerDefinition.<init>(AbstractElementContainerDefinition.java:79)
at org.jooq.util.AbstractElementContainerDefinition.<init>(AbstractElementContainerDefinition.java:75)
at org.jooq.util.AbstractTableDefinition.<init>(AbstractTableDefinition.java:68)
at org.jooq.util.mysql.MySQLTableDefinition.<init>(MySQLTableDefinition.java:70)
at org.jooq.util.mysql.MySQLDatabase.getTables0(MySQLDatabase.java:256)
at org.jooq.util.AbstractDatabase.getTables(AbstractDatabase.java:1137)
at org.jooq.util.AbstractDatabase.getTable(AbstractDatabase.java:1163)
at org.jooq.util.AbstractDatabase.getTable(AbstractDatabase.java:1158)
at org.jooq.util.mysql.MySQLDatabase.getEnums0(MySQLDatabase.java:295)
at org.jooq.util.AbstractDatabase.getEnums(AbstractDatabase.java:1182)
at org.jooq.util.JavaGenerator.generateSchemaIfEmpty(JavaGenerator.java:334)
at org.jooq.util.JavaGenerator.generateCatalogIfEmpty(JavaGenerator.java:323)
at org.jooq.util.JavaGenerator.generate(JavaGenerator.java:297)
at org.jooq.util.GenerationTool.run(GenerationTool.java:610)
at org.jooq.util.GenerationTool.generate(GenerationTool.java:199)
at org.jooq.util.GenerationTool$generate.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at build_87hklhc6v691dvh83y5ogqnvl.run(/Users/lorenklingman/Sites/code-search-parser/build.gradle:79)
at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:74)
Finally, for completeness, here are the files generated for all databases.
I believe you've run into this problem here: #5213
Be sure to always use the exact upper/lower case writing of your database name also in the jOOQ configuration. Also, there are some caveats with case sensitivity in MySQL and MariaDB, depending on the operating system. These caveats can affect other tools than jOOQ. The relevant info is also in #5213.

rolling or backing up logfile on startup dropwizard

I need to rotate or backup the current log file on startup but can't work out how to do it.
I have a typical log config:
appenders:
- type: console
threshold: INFO
- type: file
threshold: DEBUG
logFormat: "%-6level [%d{HH:mm:ss.SSS}] [%t] %logger{5} - %X{code} %msg %n"
currentLogFilename: /var/log/dw-service.log
archive: true
archivedLogFilenamePattern: /var/log/tmp/dw-service-%d{yyyy-MM-dd}-%i.log.gz
archivedFileCount: 3
timeZone: UTC
maxFileSize: 10MB
Ideally I would like to cause an archive of the logfile immediately on startup but failing that, I would settle for getting a handle to the appender and copying the file.
I can't work out how to do either. Please could somebody offer any help?
I've got this far:
private void backupLogFile() {
ch.qos.logback.classic.Logger log = (ch.qos.logback.classic.Logger)LoggerFactory.getLogger(LOG.ROOT_LOGGER_NAME);
Iterator<Appender<ILoggingEvent>> itr = log.iteratorForAppenders();
List<Appender<ILoggingEvent>> appenders = new LinkedList<Appender<ILoggingEvent>>();
while (itr.hasNext()) {
appenders.add(itr.next());
}
for(int i=0;i<appenders.size();++i){
if(appenders.get(i).getName().equals("async-file-appender"))
{
LOG.info("FOUND FILE APPENDER");
FileAppender myFile = (FileAppender)appenders.get(i);
String filename = myFile.getFile();
}
}
}
At runtime, Java tells me
Exception in thread "main" java.lang.ClassCastException: ch.qos.logback.classic.AsyncAppender incompatible with ch.qos.logback.core.FileAppender
I can't work out how to get the configured currentLogFilename, anybody know how do this?
Thanks

Gradle loads mysql-connector jar but no dbunit jar as external dependencies, why?

Please give me some lights about what I'm doing wrong here. First of all I'm newbie with Gradle and Groovy and for learning purposes I'm playing with them and DBUnit.
I tried the code listed below, my goal is to generate a dataset getting the data from a mysql db.
import groovy.sql.Sql
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;
repositories {
mavenCentral()
}
configurations {
dbunit
}
dependencies {
dbunit 'dbunit:dbunit:2.2',
'junit:junit:4.11',
'mysql:mysql-connector-java:5.1.25'
}
URLClassLoader loader = GroovyObject.class.classLoader
configurations.dbunit.each { File file -> loader.addURL(file.toURL()) }
task listJars << {
configurations.dbunit.each { File file -> println file.name }
}
task listTables << {
getConnection("mydb").eachRow('show tables') { row -> println row[0] }
}
task generateDataSet << {
def IDatabaseConnection conn = new DatabaseConnection(getConnection("mydb").connection)
def IDataSet fullDataSet = conn.createDataSet()
FlatXmlDataSet.write(fullDataSet, new FileOutputStream("full.xml"))
}
static Sql getConnection(db) {
def props = [user: 'dbuser', password: 'userpass', allowMultiQueries: 'true'] as Properties
def url = (db) ? 'jdbc:mysql://host:3306/'.plus(db) : 'jdbc:mysql://host:3306/'
def driver = 'com.mysql.jdbc.Driver'
Sql.newInstance(url, props, driver)
}
What is weird to me is that all MySQL methods work well, I can get the list of tables and for instance the connection was done well so the mysql-connector-java.jar is being loaded (I think), but when I add the DBUnit stuff (import libs and the generateDataSet method) it seems the dbunit jar is not available for the script, I got the following errors:
FAILURE: Build failed with an exception.
* Where:
Build file '/home/me/tmp/dbunit/build.gradle' line: 5
* What went wrong:
Could not compile build file '/home/me/tmp/dbunit/build.gradle'.
> startup failed:
build file '/home/me/tmp/dbunit/build.gradle': 5: unable to resolve class org.dbunit.dataset.xml.FlatXmlDataSet
# line 5, column 1.
import org.dbunit.dataset.xml.FlatXmlDataSet;
^
build file '/home/me/tmp/dbunit/build.gradle': 2: unable to resolve class org.dbunit.database.DatabaseConnection
# line 2, column 1.
import org.dbunit.database.DatabaseConnection;
^
build file '/home/me/tmp/dbunit/build.gradle': 3: unable to resolve class org.dbunit.database.IDatabaseConnection
# line 3, column 1.
import org.dbunit.database.IDatabaseConnection;
^
build file '/home/me/tmp/dbunit/build.gradle': 4: unable to resolve class org.dbunit.dataset.IDataSet
# line 4, column 1.
import org.dbunit.dataset.IDataSet;
^
4 errors
But if I call the listJars task, I got this:
:listJars
junit-4.11.jar
mysql-connector-java-5.1.25.jar
hamcrest-core-1.3.jar
xercesImpl-2.6.2.jar
xmlParserAPIs-2.6.2.jar
junit-addons-1.4.jar
poi-2.5.1-final-20040804.jar
commons-collections-3.1.jar
commons-lang-2.1.jar
commons-logging-1.0.4.jar
dbunit-2.2.jar
BUILD SUCCESSFUL
Which in my understanding means all those jars were loaded and are available for the script, am I right? or am I doing something wrong with the class loader stuff?
Thanks very much.
The GroovyObject.class.classLoader.addURL hack is not the right way to add a dependency to the build script class path. It's just sometimes necessary to get JDBC drivers to work with Groovy (long story). Here is how you add a dependency to the build script class path:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "some:library:1.0"
}
}
// library can be used in the rest of the build script
You can learn more about this in the Gradle User Guide.