2

In my enterprise application, i have several background jobs to fulfill various business requirement.

eg :- Bill generation job,Activate package job etc.

Here i need to specific custom logging to validate the job status,job input,if fails then reason etc.

How can i achieve above requirement , i don't need other application logs, just need logs related to jobs.

I need something like this in code.

For eg.

log.info ("Job inputs")

log.info(Job success status)

In log file output should be like:-

Info : Job inputs

Info : Job success

Community
  • 1
  • 1
Harish Bagora
  • 686
  • 1
  • 9
  • 26

1 Answers1

0

Create a file logback-spring.xml in resources folder with content:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <property name="LOGS" value="./logs"/>

    <appender name="Console"
              class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}):
                %msg%n%throwable
            </Pattern>
        </layout>
    </appender>

    <appender name="RollingFile"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOGS}/spring-boot-logger.log</file>
        <encoder
                class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
        </encoder>

        <rollingPolicy
                class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- rollover daily and when the file reaches 10 MegaBytes -->
            <fileNamePattern>${LOGS}/archived/spring-boot-logger-%d{yyyy-MM-dd}.%i.log
            </fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                    class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
    </appender>

    <!-- LOG everything at INFO level -->
    <root level="info">
        <appender-ref ref="Console"/>
    </root>

    <logger name="desired-package-name" level="info" additivity="false">
        <appender-ref ref="RollingFile"/>
    </logger>

</configuration>
Ebrahim Pasbani
  • 9,168
  • 2
  • 23
  • 30
  • That file already have in my application to manage all others log. but here i need custom logs only for my background jobs . if i can be able to write all custom logs in text file somewhere. – Harish Bagora May 15 '19 at 13:51
  • @HarishBagora So create a appender with custom location and settings and set it for you package – Ebrahim Pasbani May 15 '19 at 13:53
  • Could you please suggest me https://stackoverflow.com/questions/15758685/how-to-write-logs-in-text-file-when-using-java-util-logging-logger I think this link's answer will be able to solve my problem ? OR any drawback of that ? – Harish Bagora May 15 '19 at 13:56
  • @HarishBagora I think that link is not good. I disagree with hardcodding. What is the problem with a custom and separate appender in `logback-spring.xml`? – Ebrahim Pasbani May 15 '19 at 14:00