1

I am trying to route the console logging to a file. Though, I was able to generate the log file but it's not capturing the output generated from logger.info . The output is getting printed on the console but the same is not reflecting in the log file.

application.properties :

logging.file.path= log
logging.pattern.file= [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

Code :

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;
@Service
public class GetImageFiles {

    @Value("${app.images.base.url}")
    String imagesBaseUrl;

    @Autowired
    DataAccessor dataAccessor;

    private static Logger logger =  LoggerFactory.getLogger(GetImageFiles.class);

    public void getImageDetails(String sessionID, String imageId, String articleId){

        HashMap<String, String> map = new HashMap<>();
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("X-Chorus-Session",sessionID);


        HttpEntity<String> request = new HttpEntity<>(null, headers);

        String imageFilesUrl = imagesBaseUrl + "/" + imageId;

        ResponseEntity<String> responseEntity = restTemplate.exchange(imageFilesUrl, HttpMethod.GET, request, String.class);

        JSONObject jsonObject = new JSONObject(responseEntity.getBody());
        String imageName = jsonObject.get("filename").toString();

        JSONObject thumbnails = (JSONObject) jsonObject.get("thumbnails");
        JSONObject thumbnailsSize = (JSONObject) thumbnails.get("large");
        String imageUrl = thumbnailsSize.get("url").toString();

        map.put(imageName, imageUrl);

        for(Map.Entry<String, String > mapValue : map.entrySet()) {
            if (mapValue.getKey().lastIndexOf(".") > 0) {
                String imageMapKey = mapValue.getKey().substring(0, mapValue.getKey().lastIndexOf("."));
                if (imageMapKey.equals(articleId)) {
                    logger.info("Processing : The image name is : " + mapValue.getKey() + " and the imager URL is :" + mapValue.getValue() + "  for article Id is :" + articleId);
                    dataAccessor.updateImageDetails(mapValue.getKey(), mapValue.getValue(), articleId);
                }
            }
        }
    }

}

pom.xml :

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.17.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.17.1</version>
        </dependency>

Console output ..

2022-03-09 17:17:00.110  INFO 32900 --- [  restartedMain] o.s.b.w.e.t.TomcatWebServer              : Tomcat started on port(s): 8080 (http) with context path ''
2022-03-09 17:17:00.122  INFO 32900 --- [  restartedMain] c.a.t.ThirdlightApplication              : Started ThirdlightApplication in 11.293 seconds (JVM running for 12.775)
Total Article Ids to be processed :10
There is no image ids to be processed for article id :52KTM41
There is no image ids to be processed for article id :52LGY10
There is no image ids to be processed for article id :52NAZ41
There is no image ids to be processed for article id :52NBT60
There is no image ids to be processed for article id :52NEK40
There is no image ids to be processed for article id :52QAZ30
There is no image ids to be processed for article id :54LAQ01
There is no image ids to be processed for article id :54LAQ20
There is no image ids to be processed for article id :56NAK43
There is no image ids to be processed for article id :56NEU40
The process has been completed !

spring.log :

[INFO ] 2022-03-09 17:20:49.920 [restartedMain] ThirdlightApplication - Starting ThirdlightApplication using Java 11.0.7 on LHTU05CD9032TMM with PID 6456 (C:\Users\psingh69\Downloads\thirdlight\target\classes started by psingh69 in C:\Users\psingh69\Downloads\thirdlight)
[INFO ] 2022-03-09 17:20:49.933 [restartedMain] ThirdlightApplication - No active profile set, falling back to 1 default profile: "default"
[INFO ] 2022-03-09 17:20:50.000 [restartedMain] DevToolsPropertyDefaultsPostProcessor - Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
[INFO ] 2022-03-09 17:20:50.000 [restartedMain] DevToolsPropertyDefaultsPostProcessor - For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
[INFO ] 2022-03-09 17:20:59.846 [restartedMain] TomcatWebServer - Tomcat initialized with port(s): 8080 (http)
[INFO ] 2022-03-09 17:20:59.994 [restartedMain] ServletWebServerApplicationContext - Root WebApplicationContext: initialization completed in 9993 ms
[INFO ] 2022-03-09 17:21:00.670 [restartedMain] OptionalLiveReloadServer - LiveReload server is running on port 35729
[INFO ] 2022-03-09 17:21:00.723 [restartedMain] TomcatWebServer - Tomcat started on port(s): 8080 (http) with context path ''
[INFO ] 2022-03-09 17:21:00.742 [restartedMain] ThirdlightApplication - Started ThirdlightApplication in 11.588 seconds (JVM running for 13.107)
Praveenks
  • 1,436
  • 9
  • 40
  • 79
  • _"Total Article Ids to be processed :10"_: this message is not logged (notice the lack of formatting), it is printed to `System.out`. – Piotr P. Karwasz Mar 09 '22 at 21:55

2 Answers2

1

In my case I had to put the configuration file "logback.xml" inside the folder src/main/resources...

It is highly customizable but you can start with something like this (both STDOUT - the console - and a FILE are simultaneously written):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE logback>
<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%date{yyyy-MM-dd HH:mm:ss} [%thread] %level %logger{0} - %msg \(%file:%line\)%n</pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>DEBUG</level>
        </filter>
    </appender>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>logs/yourappname.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>logs/yourappname.%d{yyyy-MM-dd}.log.tar.gz</fileNamePattern>
            <maxHistory>7</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd'T'HH:mm:ss'Z'} - %m%n</pattern>
        </encoder>
    </appender>

    <root level="INFO" additivity="false">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </root>

</configuration>

The example above writes an "yourappname.log" file inside the "/logs" folder (C:\logs\yourappname.log if you're using Windows and your app is running on drive C:, on /logs/yourappname.log if you're in Linux or MacOS). Furthermore, the above configuration file provides a time-based LogRotation mechanism (which means that, at regular time intervals - 7 days in this case - the logfile is compressed and archived in a tar.gz fashion and a new one is started fresh.)

I have no idea about the opportunity to put that configuration string inside the application.properties, sorry...

Slightly different from yours, my pom.xml refers to an alternative log framework (ch.qos.logback), here it is:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.36</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.11</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
    <version>1.2.11</version>
</dependency>
Devesh
  • 11
  • 1
1

It is the year of IntelliJ IDEA 2023.1.1 (CE) and Springboot version 3.0.6 and, despite the super-rich documentation, using Logback (the default logging package that comes with Springboot), to log to a file (instead of logging to the console), proved to be a nightmare for me.

I finally managed to make it work, so for the benefit of all, I am posting the key obstacles solved:

  1. application.properties is critical for file logging to work. It should be located under src/main/resource.
  2. application.properties should contain separate directives for the log file location and for its name (the "Unused property" warnings should be ignored):
logging.file.path=.
logging.file=myapp.log
  1. logback-spring.xml should be located under src/main/resource (same place as 'application.properties')
  2. Here is my working sample:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
   <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
       <file>myapp.log</file>
       <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
           <fileNamePattern>myLogFile.%d{yyyy-MM-dd}.log</fileNamePattern>
           <maxHistory>30</maxHistory>
       </rollingPolicy>
       <encoder>
           <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
       </encoder>
   </appender>

   <root level="INFO">
       <appender-ref ref="FILE" />
   </root>
</configuration>
Introspective
  • 554
  • 2
  • 5
  • 13