Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
Logback configuration mechanism which can be used in logback.groovy f…
Browse files Browse the repository at this point in the history
…iles in microservices
  • Loading branch information
dst committed Mar 13, 2015
1 parent 61ade6f commit 47fadf4
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 10 deletions.
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')
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import org.springframework.core.io.Resource;
import org.springframework.security.crypto.encrypt.TextEncryptor;

import java.io.File;

@Configuration
@Profile("!test")
public class ExternalPropertiesConfiguration implements ApplicationContextInitializer<ConfigurableApplicationContext>, Ordered {
Expand All @@ -26,7 +24,7 @@ public class ExternalPropertiesConfiguration implements ApplicationContextInitia
@Bean
public FileSystemLocator fileSystemLocator() {
return new FileSystemLocator(
findPropertiesFolder(),
PropertiesFolderFinder.find(),
appCoordinates(),
textEncryptor == null ? new FailsafeTextEncryptor() : textEncryptor);
}
Expand All @@ -36,12 +34,6 @@ public AppCoordinates appCoordinates() {
return AppCoordinates.defaults(microserviceConfig);
}

private File findPropertiesFolder() {
final File defaultConfigDirectory = new File(System.getProperty("user.home"), "config");
final String configFolder = PropertyUtils.getProperty(AppCoordinates.CONFIG_FOLDER, defaultConfigDirectory.getAbsolutePath());
return new File(configFolder);
}

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
}
Expand Down
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;
}
}
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
}
}
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')
}
}
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

0 comments on commit 47fadf4

Please sign in to comment.