4

I am currently using Spring Boot in my application and I know that it uses Logback as its default logging implementation.

Currently in my applications.properties file I have the following:

#some other properties

#logging details
logging.path= path/to/my/log/folder

This currently logs to a file: spring.log in my output folder.

How can I change this file so that it contains a time-stamp and date of when it was created?

E.g - "my-application-log-DATE-TIME.log"

java123999
  • 6,974
  • 36
  • 77
  • 121

3 Answers3

0

Drop the .properties file style configuration and use logback's proper one - logback.xml. Spring Boot is fully prepared for this! Here's an example:

<property name="logPattern" value="%d %-5level %logger{35} - %msg%n"/>
<property name="logEncoding" value="UTF-8"/>
<property name="logDirectory" value="logs"/>

<appender name="fileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${logDirectory}/myapplication.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>${logDirectory}/myapplication_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>30MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>
    <encoder>
        <charset>${logEncoding}</charset>
        <pattern>${logPattern}</pattern>
    </encoder>
</appender>

<logger name="org.springframework" level="warn"/>

<root level="INFO">
    <appender-ref ref="fileAppender"/>
</root>

This will not just add a timestamp to the log file, but will help you with log rotation as well (e.g. it'll keep old logs until they reach a certain size, etc).

  • Thanks, where do I put this in my project? – java123999 Apr 13 '16 at 15:19
  • Assuming you're having a maven-based project, just create a file called logback.xml and put it under src/main/resources, then copy the contents into that file. Also, i made a mistake and did not copy the whole file here... Start the file with and end it with to make it a proper XML. – Laszlo Merczel Apr 13 '16 at 20:25
  • 1
    Check this link for a couple complete examples for logback configuration. http://www.mkyong.com/logging/logback-xml-example/ – Laszlo Merczel Apr 13 '16 at 20:26
  • I implemented the Same code but I cant get files of defined max size, also the rolled files are doubling in size subsequently for each file, am I missing something. – Vraj Solanki Jul 06 '16 at 13:07
0

You are in the right way to do it. Use the property support of Spring Boot to custom you application.properties file.

    // application.properties file
app.name=appname
logging.path=logpath
logging.file=${appname}-{timestamp}.log

In the other hand in your backing class, you can create an url helper to get the property value :

    /**
 * Helper class to get configured urls.
 */
@Component
public class UrlHelper {

    @Inject
    private Environment env;

    private static final String LOGGING_TIMESTAMP = "{timestamp}";

private static String loggingFileUrlPattern;

/**
     * Initializes properties.
     */
    @PostConstruct
    public void initProperties() {

        loggingFileUrlPattern = env.getRequiredProperty("logging.file");
    }

/**
     * Formats the loggin Url pattern application property with the current
     * timestamp
     * 
     * @param timestamp
     *            current timestamp.
     * @return The complete logging file url.
     */

    public static String buildLoggingFileUrl(String timestamp) {
        return loggingFileUrlPattern.replace(LOGGING_FILE_URL, timestamp);
    }
}

Hope it helps !

Hassam Abdelillah
  • 2,246
  • 3
  • 16
  • 37
  • Results in `No context given for c.q.l.core.rolling.SizeAndTimeBasedRollingPolicy` – Zon Sep 09 '20 at 15:10
-1

In the appender, try to add this:

<layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
            </Pattern>      </layout>
Zizouz212
  • 4,908
  • 5
  • 42
  • 66
Mukesh
  • 540
  • 1
  • 9
  • 13