1. Project Clover database Wed Jan 17 2024 23:40:18 CST
  2. Package org.europa.together.service

File LoggingService.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

2
13
4
1
92
53
5
0.38
3.25
4
1.25

Classes

Class Line # Actions
LoggingService 28 13 0% 5 0
1.0100%
 

Contributing tests

This file is covered by 5 tests. .

Source view

1    package org.europa.together.service;
2   
3    import java.io.File;
4    import java.io.IOException;
5    import java.nio.file.Files;
6    import java.nio.file.Paths;
7    import java.nio.file.StandardCopyOption;
8    import org.apiguardian.api.API;
9    import static org.apiguardian.api.API.Status.STABLE;
10    import org.europa.together.application.LogbackLogger;
11    import org.europa.together.application.SaxTools;
12    import org.europa.together.business.Logger;
13    import org.europa.together.business.XmlTools;
14    import org.europa.together.domain.LogLevel;
15    import org.europa.together.utils.Constraints;
16    import org.europa.together.utils.FileUtils;
17    import org.springframework.stereotype.Service;
18   
19    /**
20    * Service implementation for the LogbackLogger.
21    *
22    * @author elmar.dott@gmail.com
23    * @version 1.0
24    * @since 1.0
25    */
26    @API(status = STABLE, since = "1.1")
27    @Service
 
28    public final class LoggingService {
29   
30    private static final long serialVersionUID = 201L;
31    private static final Logger LOGGER = new LogbackLogger(LoggingService.class);
32   
33    /**
34    * Constructor.
35    */
 
36  2 toggle @API(status = STABLE, since = "1.1")
37    public LoggingService() {
38  2 LOGGER.log("instance class", LogLevel.INFO);
39    }
40   
41    /**
42    * Creates for an application a external log configuration. Copy the
43    * configuration file for the Logger from the classpath to the application
44    * directory.
45    *
46    * @throws java.io.IOException
47    */
 
48  1 toggle @API(status = STABLE, since = "1.1")
49    public void createLogConfiguration() throws IOException {
50  1 String destination = Constraints.SYSTEM_APP_DIR + "/logback.xml";
51  1 LOGGER.log("Copy File to: " + destination, LogLevel.DEBUG);
52   
53  1 Files.copy(getClass().getClassLoader()
54    .getResourceAsStream("org/europa/together/configuration/logback.xml"),
55    Paths.get(destination), StandardCopyOption.REPLACE_EXISTING);
56    }
57   
58    /**
59    * Read the log configuration from a file and return the file content.
60    *
61    * @param file as String
62    * @return configuration as String
63    * @throws java.io.IOException
64    */
 
65  2 toggle @API(status = STABLE, since = "1.1")
66    public String readLogConfiguration(final String file)
67    throws IOException {
68  2 String configuration = null;
69  2 configuration = FileUtils.readFileStream(new File(file));
70  1 return configuration;
71    }
72   
73    /**
74    * Write a configuration to a file. The file will only written when the XML
75    * is well formed.
76    *
77    * @param content as String
78    * @param file as String
79    */
 
80  2 toggle @API(status = STABLE, since = "1.1")
81    public void writeLogConfiguration(final String content, final String file)
82    throws IOException {
83  2 XmlTools xmlTools = new SaxTools();
84  2 xmlTools.parseXmlString(content);
85  2 LOGGER.log("try to update logger configuration to: " + file, LogLevel.DEBUG);
86  2 if (!xmlTools.isWellFormed()) {
87  1 throw new IOException("xml is not wellformed, file can not updated.");
88    } else {
89  1 xmlTools.writeXmlToFile(xmlTools.prettyPrintXml(), file);
90    }
91    }
92    }