This repository has been archived by the owner on May 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Logback configuration mechanism which can be used in logback.groovy f…
…iles in microservices
- Loading branch information
Showing
6 changed files
with
169 additions
and
10 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
...spring-config/src/main/groovy/com/ofg/infrastructure/property/LogbackConfiguration.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.ofg.infrastructure.property | ||
|
||
import groovy.transform.CompileStatic | ||
|
||
/** | ||
* It reads Logback configuration from global.properties file | ||
* or uses a default configuration if a file doesn't exist or it | ||
* exists but doesn't contain a logger configuration. | ||
* | ||
* This class should be imported in logback.groovy file and used. | ||
*/ | ||
@CompileStatic | ||
class LogbackConfiguration { | ||
|
||
private static String DEFAULT_FILENAME = 'logs/application.log' | ||
private static String DEFAULT_LOG_PATTERN = | ||
'%d{yyyy-MM-dd HH:mm:ss.SSSZ, Europe/Warsaw} | %-5level | %X{correlationId} | %thread | %logger{1} | %m%n' | ||
private static String DEFAULT_SCAN_TIME = '1 minutes' | ||
private static String DEFAULT_ROLLING_FILENAME_PATTERN = 'logs/application.%d{yyyy-MM-dd}.log.zip' | ||
private static int DEFAULT_ROLLING_MAX_HISTORY = 7 | ||
|
||
private static final String LOG_PATTERN_KEY = 'logger.log.pattern' | ||
private static final String SCAN_TIME_KEY = 'logger.scan.time' | ||
private static final String ROLLING_FILENAME_PATTERN_KEY = 'logger.rolling.filename.pattern' | ||
private static final String FILENAME_KEY = 'logger.filename' | ||
private static final String ROLLING_MAX_HISTORY_KEY = 'logger.rolling.history.max' | ||
|
||
private Properties globalProperties | ||
|
||
String getLogPattern() { | ||
return globalProperty(LOG_PATTERN_KEY) ?: DEFAULT_LOG_PATTERN | ||
} | ||
|
||
String getScanTime() { | ||
return globalProperty(SCAN_TIME_KEY) ?: DEFAULT_SCAN_TIME | ||
} | ||
|
||
String getRollingFilenamePattern() { | ||
return globalProperty(ROLLING_FILENAME_PATTERN_KEY) ?: DEFAULT_ROLLING_FILENAME_PATTERN | ||
} | ||
|
||
String getLoggerFilename() { | ||
return globalProperty(FILENAME_KEY) ?: DEFAULT_FILENAME | ||
} | ||
|
||
int getRollingMaxHistory() { | ||
String maxHistory = globalProperty(ROLLING_MAX_HISTORY_KEY) | ||
return maxHistory != null ? Integer.parseInt(maxHistory) : DEFAULT_ROLLING_MAX_HISTORY | ||
} | ||
|
||
private String globalProperty(String propKey) { | ||
return globalProperties()[propKey] | ||
} | ||
|
||
private Properties globalProperties() { | ||
if (globalProperties == null) { | ||
Properties props = new Properties() | ||
File globalPropsFile = globalPropertiesFile() | ||
if (globalPropsFile.exists()) { | ||
globalPropsFile.withInputStream { | ||
stream -> props.load(stream) | ||
} | ||
} | ||
globalProperties = props | ||
} | ||
return globalProperties | ||
} | ||
|
||
private File globalPropertiesFile() { | ||
return new File(PropertiesFolderFinder.find(), '/common/global.properties') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...a-spring-config/src/main/java/com/ofg/infrastructure/property/PropertiesFolderFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.ofg.infrastructure.property; | ||
|
||
import static com.ofg.infrastructure.property.AppCoordinates.CONFIG_FOLDER; | ||
|
||
import java.io.File; | ||
|
||
class PropertiesFolderFinder { | ||
|
||
public static final File DEFAULT_CONFIG_DIR = new File(System.getProperty("user.home"), "config"); | ||
|
||
static File find() { | ||
final String configFolder = PropertyUtils.getProperty(CONFIG_FOLDER, null); | ||
return configFolder != null ? new File(configFolder) : DEFAULT_CONFIG_DIR; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...ng-config/src/test/groovy/com/ofg/infrastructure/property/LogbackConfigurationSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.ofg.infrastructure.property | ||
|
||
import spock.lang.Specification | ||
import spock.util.environment.RestoreSystemProperties | ||
|
||
import static com.ofg.infrastructure.property.AppCoordinates.CONFIG_FOLDER | ||
|
||
class LogbackConfigurationSpec extends Specification { | ||
|
||
LogbackConfiguration config = new LogbackConfiguration() | ||
|
||
def 'log pattern should contain correlationId'() { | ||
expect: | ||
config.getLogPattern().contains('correlationId') | ||
} | ||
|
||
def 'scan time should be in minutes'() { | ||
expect: | ||
config.getScanTime().contains('minutes') | ||
} | ||
|
||
def 'rolled files should be zipped'() { | ||
expect: | ||
config.getRollingFilenamePattern().endsWith('zip') | ||
} | ||
|
||
def 'log keyword should be present in logger filename'() { | ||
expect: | ||
config.getLoggerFilename().contains('log') | ||
} | ||
|
||
def 'rolling max history should be positive'() { | ||
expect: | ||
config.getRollingMaxHistory() > 0 | ||
} | ||
|
||
@RestoreSystemProperties | ||
def 'should read logger configuration from global.properties file'() { | ||
given: | ||
System.setProperty(CONFIG_FOLDER, getConfigFolder()) | ||
expect: | ||
config.getLoggerFilename() == 'logs/fake123Logger.log' | ||
} | ||
|
||
private String getConfigFolder() { | ||
return getClass().getResource('/test-config-dir').file | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...-config/src/test/groovy/com/ofg/infrastructure/property/PropertiesFolderFinderSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.ofg.infrastructure.property | ||
|
||
import spock.lang.Specification | ||
import spock.util.environment.RestoreSystemProperties | ||
|
||
import static com.ofg.infrastructure.property.AppCoordinates.CONFIG_FOLDER | ||
import static com.ofg.infrastructure.property.PropertiesFolderFinder.DEFAULT_CONFIG_DIR | ||
|
||
class PropertiesFolderFinderSpec extends Specification { | ||
|
||
PropertiesFolderFinder propertiesFolderFinder = new PropertiesFolderFinder() | ||
|
||
def 'should return default config folder when CONFIG_FOLDER is not set'() { | ||
expect: | ||
propertiesFolderFinder.find() == DEFAULT_CONFIG_DIR | ||
} | ||
|
||
@RestoreSystemProperties | ||
def 'should return config folder from CONFIG_FOLDER variable'() { | ||
given: | ||
System.setProperty(CONFIG_FOLDER, 'properties') | ||
|
||
expect: | ||
propertiesFolderFinder.find() == new File('properties') | ||
} | ||
} |
8 changes: 7 additions & 1 deletion
8
micro-infra-spring-config/src/test/resources/test-config-dir/common/global.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
global.prop.key=global prop value | ||
global.default.prop.key=common default prop value | ||
custom.global.key=custom global prop value | ||
custom.global.key=custom global prop value | ||
|
||
logger.filename=logs/fake123Logger.log | ||
logger.log.pattern=fake123LoggerPattern| %m%n | ||
logger.scan.time=123 minutes | ||
logger.rolling.filename.pattern=logs/fake123Logger.%d{yyyy-MM-dd}.log.zip | ||
logger.rolling.history.max=123 |