Multiple Grails Application + tomcat-slf4j-logback, how to get application name into logfile -


we deploying several grails (2.4.4) applications tomcat 7 server , have configured logging described here tomcat-slf4j-logback.

the desire configure grails applications via 1 logback.xml file log applications single file, discriminate log messages application name defined in grails' application.properties:

#grails metadata file app.name=my-application 

our logback.xml appender looks this:

<appender name="file"           class="ch.qos.logback.core.rolling.rollingfileappender">     <file>${catalina.base}/logs/all.log</file>     <append>true</append>     <encoder>         <charset>utf-8</charset>         <pattern>%d [%x{appname:-null}] %-5level %logger : %msg%n</pattern>     </encoder>     ... </appender> 

as can see pulling 'appname' mdc (mapped diagnostic context). placed there grails filter in grails plugin shared applications looks application.properties file. produces output in log file similar to:

13:34:18.796 [application-1] debug a.b.c.myclass : hello 13:34:18.797 [application-2] debug a.b.c.myclass : hello 13:34:18.798 [null] debug b.c.d.otherclass : no app name 13:34:18.798 [application-3] debug a.b.c.myclass : hello 13:34:18.799 [null] debug b.c.d.otherclass : no app name 

this solution lacking due threadlocal nature of mdc. classes executing outside of request/response threads don't have access same mdc 'appname' , instead print '[null]'.

i have tried following in logback.xml

<property resource="application.properties" /> 

which supposed load application.properties classpath gives:

  13:34:18.796 [app.name_is_undefined] debug a.b.c.myclass : hello 

which think means logback can't find application.properties files (they packaged in web-inf\classes\ in each of wars)...

i've tried locally using logback.groovy ala:

// logback.groovy // read in grails application.properties def props = new properties() new file("application.properties").withinputstream {      stream -> props.load(stream)  }  def slurper = new configslurper().parse(props)  // application name. string grailsappname = slurper.app.name  appender("console", consoleappender) { encoder(patternlayoutencoder) {   pattern = "%d [${grailsappname ?: 'application name not set'}]  %-5level %logger - %msg%n"] } 

}

and works, long set 'logback.configurationfile' point don't think replacing single logback.xml tomcat 7 server logback.groovy work (but next on list try)

other things i've tried

  • using bootstrap.groovy access logback context , 'putting' 'app.name' context
  • using bootstrap.groovy mdc.put('app.name', ""application-1")
  • i've read contextselectors http://logback.qos.ch/manual/contextselector.html , jndi contextselector interesting, don't want duplicate configurations, add property in each application can access appender pattern

any appreciated

steps configure:

  1. add below line in grails-app/conf/application.yml or external application configuration file:

     logging:  config: "c:/application/config/logback.groovy"    file: "app.log"    path: "c:/application/logs"   
  2. put configuration file in above provided config path(in above e.g c:/application/config/logback.groovy):

ex.:

import grails.util.buildsettings   import grails.util.environment    import ch.qos.logback.classic.encoder.patternlayoutencoder   import ch.qos.logback.core.consoleappender   import ch.qos.logback.core.fileappender    import static ch.qos.logback.classic.level.debug    def targetdir = system.getproperty("log_path")   def targetfile = system.getproperty("log_file")    appender("file", fileappender) {   file = "${targetdir}/${targetfile}"   encoder(patternlayoutencoder) {   pattern = "%date %level %logger - %msg%n"   }   } appender("stdout", consoleappender) {   encoder(patternlayoutencoder) {   pattern = "%msg%n"   }   }   root(info, ["file", "stdout"])   

all applications can point same configuration file following first step. so, logs generated according path , file specified in step 1.

system properties logging system takes care of creating you:

•   ${pid} current process id.   •   ${log_file} if logging.file set in boot’s external configuration.   •   ${log_path} if logging.path set (representing directory log files   live in).   •   ${log_exception_conversion_word} if logging.exception-conversion-word   set in boot’s external configuration.   

so can use them in logback.groovy file shown above system.getproperty("log_path").
references:
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -