Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

struct logs with gz rotation #18

Merged
merged 7 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -42,6 +40,9 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.web.filter.CorsFilter;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Configuration
@ConfigurationProperties
Expand Down Expand Up @@ -148,8 +149,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 Expand Up @@ -181,4 +187,4 @@ public TestbedApiHibernateUtil getHibernateUtil(
hibernateUtil.setDatabaseProps(databaseProps);
return hibernateUtil;
}
}
}
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;
}

}