Skip to content

Commit

Permalink
Merge pull request #18 from ga4gh/yp_struct_logs
Browse files Browse the repository at this point in the history
struct logs with gz rotation
  • Loading branch information
Jeremy Adams authored Mar 16, 2022
2 parents 3d6abc0 + 1e6a707 commit cc41f0b
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 6 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ dependencies {
implementation 'org.xerial:sqlite-jdbc:3.8.11.2'
implementation 'com.vladmihalcea:hibernate-types-52:2.14.0'
implementation 'org.ga4gh:ga4gh-starter-kit-common:0.5.6'
implementation 'net.logstash.logback:logstash-logback-encoder:7.0.1'

testCompileOnly 'org.projectlombok:lombok:1.18.22'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.22'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package org.ga4gh.testbed.api.app;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import ch.qos.logback.classic.Logger;
import org.apache.catalina.connector.Connector;
import org.apache.commons.cli.Options;
import org.ga4gh.starterkit.common.config.DatabaseProps;
import org.ga4gh.starterkit.common.config.ServerProps;
import org.ga4gh.starterkit.common.hibernate.HibernateEntity;
import org.ga4gh.starterkit.common.util.CliYamlConfigLoader;
import org.ga4gh.starterkit.common.util.DeepObjectMerger;
import org.ga4gh.starterkit.common.util.logging.LoggingUtil;
import org.ga4gh.starterkit.common.util.webserver.AdminEndpointsConnector;
import org.ga4gh.starterkit.common.util.webserver.AdminEndpointsFilter;
import org.ga4gh.starterkit.common.util.webserver.CorsFilterBuilder;
Expand All @@ -30,6 +27,7 @@
import org.ga4gh.testbed.api.model.TestbedTest;
import org.ga4gh.testbed.api.model.TestbedVersion;
import org.ga4gh.testbed.api.utils.hibernate.TestbedApiHibernateUtil;
import org.ga4gh.testbed.api.utils.logging.TestbedLoggingUtil;
import org.ga4gh.testbed.api.utils.requesthandler.report.CreateReportHandler;
import org.ga4gh.testbed.api.utils.requesthandler.report.ShowReportHandler;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -45,6 +43,9 @@
import org.springframework.context.annotation.Scope;
import org.springframework.web.context.annotation.RequestScope;
import org.springframework.web.filter.CorsFilter;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Configuration
@ConfigurationProperties
Expand Down Expand Up @@ -144,8 +145,13 @@ public DatabaseProps getDatabaseProps(
* ****************************** */

@Bean
public LoggingUtil loggingUtil() {
return new LoggingUtil();
public TestbedLoggingUtil testbedloggingUtil() {
return new TestbedLoggingUtil();
}

@Bean
public Logger getLogger(@Autowired TestbedLoggingUtil testbedLoggingUtil) {
return testbedLoggingUtil.getLogger();
}

/* ******************************
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package org.ga4gh.testbed.api.utils.logging;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.ConsoleAppender;
import ch.qos.logback.core.rolling.RollingFileAppender;
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy;
import net.logstash.logback.encoder.LogstashEncoder;
import net.logstash.logback.fieldnames.LogstashFieldNames;
import org.ga4gh.starterkit.common.config.ServerProps;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.PostConstruct;

public class TestbedLoggingUtil{

@Autowired
private ServerProps serverProps;

private boolean configured;

private Logger logger;

public TestbedLoggingUtil() {
configured = false;
}

@PostConstruct
public void buildLogger() {
setLogger((Logger) LoggerFactory.getLogger(getClass()));

LoggerContext loggerContext = logger.getLoggerContext();
loggerContext.reset();

// logback logstash encoder
LogstashEncoder logstashEncoder = new LogstashEncoder();
logstashEncoder.setIncludeCallerData(true);
LogstashFieldNames fieldnames = logstashEncoder.getFieldNames();

// To ignore the default fields - level value, version and thread fields,
// set them as null
fieldnames.setLevelValue(null);
fieldnames.setVersion(null);
fieldnames.setThread(null);

// rename existing field names
fieldnames.setLevel("level");
fieldnames.setTimestamp("utc_timestamp");
fieldnames.setLogger("logger");
fieldnames.setCallerClass("class_name");
fieldnames.setCallerMethod("method_name");
fieldnames.setCallerFile("file_name");
fieldnames.setCallerLine("line_number");

// set the new fieldnames
logstashEncoder.setFieldNames(fieldnames);

// set the timezone as UTC
logstashEncoder.setTimeZone("UTC");

if (getServerProps().getLogFile() == null) {

// write logs to console
ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<>();
appender.setContext(loggerContext);
appender.setName("consoleAppender");
appender.setEncoder(logstashEncoder);
logstashEncoder.start();
appender.start();
getLogger().addAppender(appender);
} else {

// write logs to file

// zip rotate at the start of every day - UTC timezone
TimeBasedRollingPolicy<ILoggingEvent> policy = new TimeBasedRollingPolicy<>();
policy.setFileNamePattern(("./logs/" + getServerProps().getLogFile()).replace(".log",".%d{yyyy-MM-dd, UTC}.log.gz"));

// keep history of log files from the past 30 days.
policy.setMaxHistory(30);
policy.setContext(loggerContext);

// Add to the appender
RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<>();
appender.setContext(loggerContext);
appender.setName("rollingFileAppender");

// add the log files to ./logs directory
appender.setFile("./logs/"+getServerProps().getLogFile());
appender.setAppend(true);
appender.setPrudent(false);
appender.setRollingPolicy(policy);
appender.setEncoder(logstashEncoder);
policy.setParent(appender);
policy.start();
logstashEncoder.start();
appender.start();
getLogger().addAppender(appender);
}
setLogLevel();
setConfigured(true);
}

private void setLogLevel() {
Level level;

switch(serverProps.getLogLevel()) {
case TRACE:
level = Level.TRACE;
break;
case DEBUG:
level = Level.DEBUG;
break;
case INFO:
level = Level.INFO;
break;
case WARN:
level = Level.WARN;
break;
case ERROR:
level = Level.ERROR;
break;
default:
level = Level.DEBUG;
break;
}
getLogger().setLevel(level);
}

public void setServerProps(ServerProps serverProps) {
this.serverProps = serverProps;
}

public ServerProps getServerProps() {
return serverProps;
}

public void setConfigured(boolean configured) {
this.configured = configured;
}

public boolean getConfigured() {
return configured;
}

public void setLogger(Logger logger) {
this.logger = logger;
}

public Logger getLogger() {
return logger;
}

}

0 comments on commit cc41f0b

Please sign in to comment.