maven - Make Logback write to the console locally, but to log file on server -
in web application built maven 3, there way have logback use either consoleappender
or rollingfileappender
depending where/how application running?
in production .war file deployed on tomcat 7. locally i'm running jetty maven plugin testing purposes during development.
i have logging work this:
- when run
mvn jetty:run
locally: useconsoleappender
- when
*.war
file deployed on production (tomcat 7): userollingfileappender
during local development seems comfortable have log output on console. in production instead prefer log file: catalina_base/logs/myapp.log
.
obviously use both consoleappender
, rollingfileappender
in logback.xml
. me seems unnecessarily redundant have log output on production written stdout
, log file. also, tomcat documentation sounds logging stdout on production bad practice in general.
i couldn't find nice solutions on web. there solution this?
here's current logback.xml
logs stdout
, desired log file:
<?xml version="1.0" encoding="utf-8"?> <configuration> <appender name="stdout" class="ch.qos.logback.core.consoleappender"> <encoder> <pattern>%d{hh:mm:ss.sss} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <appender name="file" class="ch.qos.logback.core.rolling.rollingfileappender"> <file>${catalina.base}/logs/myapp.log</file> <rollingpolicy class="ch.qos.logback.core.rolling.timebasedrollingpolicy"> <filenamepattern>${catalina.base}/logs/myapp.%d{yyyy-mm-dd}.log</filenamepattern> <maxhistory>30</maxhistory> </rollingpolicy> <encoder> <pattern>%d{hh:mm:ss.sss} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <logger name="com.mydomain.myapp" level="debug" /> <root level="debug"> <appender-ref ref="stdout" /> <appender-ref ref="file" /> </root> </configuration>
you can use profiles switch between environment. have 2 different logback.xml files local , prod. create logback directory inside resources directory. inside resources directory, create environment specific directory. directory name , envname inside property should match. here example.
<profiles> <profile> <id>local</id> <activation> <activebydefault>true</activebydefault> </activation> <properties> <envname>local</envname> </properties> </profile> <profile> <id>prod</id> <properties> <envname>prod</envname> </properties> </profile> </profiles> <build> <finalname>kp-prj</finalname> <outputdirectory>${project.build.directory}/classes</outputdirectory> <sourcedirectory>${project.basedir}/src/main/java</sourcedirectory> <resources> <resource> <directory>${basedir}/src/main/java</directory> <includes> <include>**/*.class</include> </includes> </resource> <resource> <directory>src/main/resources/logback/${envname}</directory> </resource> </resources> </build>
and while using running maven command specify profile.
mvn run -plocal
Comments
Post a Comment