Commit b17e279a authored by Artem Hrybeniuk's avatar Artem Hrybeniuk
Browse files

Log4j2: v2 updates

parent a242f56b
......@@ -82,7 +82,7 @@
<hibernate.validator.version>6.2.0.Final</hibernate.validator.version>
<hsqldb.version>2.6.0</hsqldb.version>
<slf4j.version>1.7.32</slf4j.version>
<log4j2.version>2.17.1</log4j2.version>
<aspectj.version>1.9.7</aspectj.version>
......@@ -126,10 +126,31 @@
</exclusions>
</dependency>
<!-- Logging dependencies -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>${commons.beanutils.version}</version>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jul</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
......@@ -137,7 +158,7 @@
<artifactId>commons-io</artifactId>
<version>${commons.io.version}</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
......@@ -156,30 +177,16 @@
<version>${commons.lang.version}</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>${commons.logging.version}</version>
<!-- Embedded in jcl-over-slf4j -->
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jcl</artifactId>
<version>${spring.framework.version}</version>
<!-- Embedded in jcl-over-slf4j -->
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.15.0</version>
</dependency>
<dependency>
<groupId>org.graylog2</groupId>
<artifactId>gelfj</artifactId>
<version>1.1.16</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
......@@ -189,7 +196,7 @@
<!-- Embedded in spring-aop -->
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
......@@ -197,7 +204,7 @@
<!-- Embedded in aspectjweaver -->
<scope>provided</scope>
</dependency>
<dependency>
<groupId>stax</groupId>
<artifactId>stax-api</artifactId>
......@@ -218,25 +225,6 @@
<version>${commons.validator.version}</version>
</dependency>
<!-- Logging dependencies -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- Hibernate dependencies -->
......@@ -268,6 +256,7 @@
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>${hsqldb.version}</version>
<scope>test</scope>
</dependency>
<dependency>
......@@ -646,6 +635,12 @@
<groupId>org.genesys-pgr</groupId>
<artifactId>transifex-client</artifactId>
<version>0.6-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.genesys-pgr</groupId>
......@@ -955,6 +950,7 @@
<showDeprecation>${show.deprecations}</showDeprecation>
<showWarnings>true</showWarnings>
<failOnError>true</failOnError>
<forceJavacCompilerUse>true</forceJavacCompilerUse>
</configuration>
</plugin>
<plugin>
......
......@@ -131,7 +131,7 @@ public class RepositoryDownloadController extends BaseController {
}
}
if (data != null) {
if (data != null && data.length > 0) {
response.setContentLength(data.length);
response.getOutputStream().write(data);
response.flushBuffer();
......
package org.genesys2.server.mvc.admin;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.List;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
......@@ -25,8 +26,6 @@ import org.springframework.web.bind.annotation.RequestParam;
@PreAuthorize("hasRole('ADMINISTRATOR')")
public class LoggerController {
private static final Logger LOG = Logger.getLogger(AdminController.class);
@Value("${paginator.default.pageSize}")
int defaultPageSize;
......@@ -50,31 +49,35 @@ public class LoggerController {
@RequestMapping(method = RequestMethod.GET, value = "/{loggerName}")
public String adjustLoggerPage(Model model, @PathVariable(value = "loggerName") String loggerName) {
LoggerContext context = (LoggerContext) LogManager.getContext(false);
Logger logger;
if (loggerName != null && "root".equalsIgnoreCase(loggerName))
logger = LogManager.getRootLogger();
if (loggerName == null || "root".equalsIgnoreCase(loggerName))
logger = context.getRootLogger();
else
logger = LogManager.getLogger(loggerName);
logger = context.getLogger(loggerName);
model.addAttribute("logger", logger);
model.addAttribute("appenders", Logger.getRootLogger().getAllAppenders());
model.addAttribute("appenders", context.getConfiguration().getAppenders().values());
return "/admin/logger/edit";
}
@RequestMapping(method = RequestMethod.POST, value = "/changeLoger")
public String changeLogger(@RequestParam(value = "loggerLevel") String loggerLevel, @RequestParam(value = "loggerName") String loggerName) {
Logger logger;
LoggerContext context = (LoggerContext) LogManager.getContext(false);
Configuration config = context.getConfiguration();
LoggerConfig loggerConfig;
if (loggerName == null || "root".equalsIgnoreCase(loggerName)) {
logger = LogManager.getRootLogger();
loggerConfig = config.getRootLogger();
} else {
logger = LogManager.getLogger(loggerName);
loggerConfig = config.getLoggerConfig(loggerName);
}
if (logger != null) {
LOG.debug("Got logger: " + logger.getName());
logger.setLevel(loggerLevel == null ? null : Level.toLevel(loggerLevel));
if (loggerConfig != null) {
loggerConfig.setLevel(loggerLevel == null ? null : Level.toLevel(loggerLevel));
context.updateLoggers();
}
return "redirect:/admin/logger/";
......@@ -82,30 +85,23 @@ public class LoggerController {
private List<Logger> getAllLoggers() {
Enumeration<Logger> en = LogManager.getCurrentLoggers();
List<Logger> loggers = new ArrayList<>();
while (en.hasMoreElements()) {
Logger logger = en.nextElement();
if (logger.getLevel() != null)
loggers.add(logger);
}
LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
Configuration config = loggerContext.getConfiguration();
loggers.add(LogManager.getRootLogger());
Collections.sort(loggers, new Comparator<Logger>() {
@Override
public int compare(Logger o1, Logger o2) {
// root logger is on the top
if (LogManager.getRootLogger() == o1) {
return -1;
}
if (LogManager.getRootLogger() == o2) {
return 1;
}
// otherwise sort by name
return o1.getName().compareTo(o2.getName());
config.getLoggers().keySet().forEach(loggerName -> loggers.add(loggerContext.getLogger(loggerName)));
loggers.sort((o1, o2) -> {
// root logger is on the top
if (loggerContext.getRootLogger() == o1) {
return -1;
}
if (loggerContext.getRootLogger() == o2) {
return 1;
}
// otherwise sort by name
return o1.getName().compareTo(o2.getName());
});
return loggers;
......@@ -113,8 +109,16 @@ public class LoggerController {
@RequestMapping(method = RequestMethod.POST, value = "/addLoger")
public String addLogger(@RequestParam(value = "nameNewLogger") String nameNewLogger, @RequestParam("loggerLevel") String loggerLevel, Model model) {
Logger logger = Logger.getLogger(nameNewLogger);
logger.setLevel(Level.toLevel(loggerLevel));
LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
Configuration config = loggerContext.getConfiguration();
org.apache.logging.log4j.Level level = org.apache.logging.log4j.Level.toLevel(loggerLevel);
LoggerConfig loggerConfig = new LoggerConfig(nameNewLogger, level, false);
Appender appender = config.getRootLogger().getAppenders().values().stream().findFirst().orElse(null);
loggerConfig.addAppender(appender, level, null);
config.addLogger(nameNewLogger, loggerConfig);
loggerContext.updateLoggers();
return "redirect:/admin/logger/";
}
......
......@@ -83,7 +83,7 @@ public class GelfConfig implements InitializingBean {
appender.setExtractStacktrace(true);
appender.setOriginHost(originHost);
appender.setLayout(new PatternLayout());
appender.setLayout(new PatternLayout("%d{ABSOLUTE} %t %5p %c{1}:%L - %m%n"));
appender.activateOptions();
this.gelfSender = appender.getGelfSender();
......
#-------------------------------------------------------------------------------
# Copyright 2014 Global Crop Diversity Trust
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#-------------------------------------------------------------------------------
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.target=System.out
log4j.appender.stdout.encoding=UTF-8
log4j.appender.stdout.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %t %5p %c{1.}:%L - %m%n
#log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %t %5p %.40c{6}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=error, stdout
log4j.category.org.genesys2=warn
log4j.category.org.genesys=warn
log4j.category.org.genesys2.server.api=warn
log4j.category.org.springframework.web.filter.CommonsRequestLoggingFilter=INFO
#log4j.category.org.genesys.custom.elasticsearch=info
#log4j.category.org.genesys2.server.service.impl.ElasticsearchServiceImpl=debug
#log4j.category.org.genesys2.server.service.impl.KPIServiceImpl=trace
log4j.category.org.genesys2.server.service.worker.KpiScheduledExecutor=info
log4j.category.liquibase=info
#log4j.category.org.hibernate.stat=info
#log4j.category.springfox=trace
#log4j.category.org.genesys2.server.servlet.controller=debug
#log4j.category.org.hibernate.cfg.Configuration=debug
#log4j.category.org.hibernate.search=debug
log4j.category.org.apache.tomcat.jdbc.pool=debug
#log4j.category.org.springframework.web.servlet.mvc=trace
#log4j.category.org.springframework.jdbc.core.JdbcTemplate=debug
#log4j.category.org.springframework.security.oauth2=trace
#log4j.category.org.springframework.security.access=trace
#log4j.category.org.springframework.security.acl=trace
#log4j.category.org.springframework.expression=trace
#log4j.category.com.hazelcast=info
#log4j.category.com.hazelcast.web=debug
#log4j.category.com.hazelcast.aws=trace
#log4j.category.org.eclipse.jetty.servlets=trace
#LocaleURLFilter
#log4j.category.org.genesys2.server.servlet.filter=debug
#log4j.category.org.genesys2.server.servlet.filter.NewGUIFilter=debug
#log4j.category.org.genesys2.spring.config.NewGUIViewResolver=trace
#log4j.category.org.genesys.filerepository.service.ftp=trace
# Internal Hibernate logging is at ERROR
log4j.category.org.hibernate.engine.jdbc=fatal
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2021 Global Crop Diversity Trust
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<Configuration>
<Appenders>
<Console name="console">
<PatternLayout pattern="%d{ABSOLUTE} %t %5p %c{1}:%L - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="org.genesys2" level="warn" />
<Logger name="org.genesys" level="warn" />
<Logger name="org.genesys2.server.api" level="warn" />
<Logger name="org.springframework.web.filter.CommonsRequestLoggingFilter" level="info" />
<Logger name="org.genesys2.server.service.worker.KpiScheduledExecutor" level="info" />
<Logger name="liquibase" level="info" />
<Logger name="org.apache.tomcat.jdbc.pool" level="debag" />
<Logger name="org.hibernate.engine.jdbc" level="fatal" />
<Root level="error">
<AppenderRef ref="console" />
</Root>
</Loggers>
</Configuration>
\ No newline at end of file
# JUL logs go to log4j2
handlers = org.apache.logging.log4j.jul.Log4jBridgeHandler
org.apache.logging.log4j.jul.Log4jBridgeHandler.propagateLevels = true
......@@ -13,7 +13,7 @@
<form method="post" class="form-horizontal" action="<c:url value="/admin/logger/changeLoger"/>">
<!-- CSRF protection -->
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
<input type="hidden" name="loggerName" value="${logger.name}" />
<input type="hidden" name="loggerName" value="${empty logger.name ? 'root' : logger.name}" />
<div class="row">
<label class="col-sm-4 control-label">
......@@ -21,7 +21,7 @@
</label>
<div class="col-sm-8">
<p class="form-control-static">
<c:out value="${logger.name}" />
<c:out value="${empty logger.name ? 'root' : logger.name}" />
</p>
</div>
</div>
......
......@@ -21,7 +21,7 @@
<c:forEach items="${loggers.content}" var="logger" varStatus="status">
<tr>
<td class="col-xs-8 form-control-static">
<a href="<c:url value="/admin/logger/${logger.name}" />"><c:out value="${logger.name}"/></a>
<a href="<c:url value="/admin/logger/${empty logger.name ? 'root' : logger.name}" />"><c:out value="${empty logger.name ? 'root' : logger.name}"/></a>
</td>
<td class="col-xs-4 text-right form-control-static"><c:out value="${logger.level}"/></td>
</tr>
......
#-------------------------------------------------------------------------------
# Copyright 2014 Global Crop Diversity Trust
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#-------------------------------------------------------------------------------
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} [%t] %5p %c{1.}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=warn, stdout
#log4j.category.org.genesys2=info
# We don't have proper ACL in tests
log4j.category.org.genesys.blocks.security=error
log4j.category.org.genesys2.server=warn
#log4j.category.org.genesys.custom.elasticsearch=trace
#log4j.category.org.genesys2.server.component.elastic=trace
#log4j.category.org.genesys2.server.service.impl.ElasticsearchServiceImpl=trace
#log4j.category.org.hibernate.tool.hbm2ddl=trace
#log4j.category.org.genesys2.server.service.impl.KPIServiceImpl=trace
#log4j.category.org.genesys.test=debug
#log4j.category.org.genesys2.server.service.worker.AccessionUploader=trace
#log4j.category.org.genesys2.server.service.worker.AccessionCounter=trace
#log4j.category.org.hibernate.cache=debug
#log4j.category.org.hibernate.jdbc=debug
#log4j.category.org.hibernate.jdbc.AbstractBatcher=debug
#log4j.category.org.springframework.beans=debug
#log4j.category.org.springframework.test.context=trace
#log4j.category.org.springframework.test.context=debug
#log4j.category.org.genesys2.server.component.elastic.ElasticQueryBuilder=trace
# Internal Hibernate logging is at ERROR
log4j.category.org.hibernate.engine.jdbc=fatal
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2021 Global Crop Diversity Trust
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<Configuration>
<Appenders>
<Console name="console">
<PatternLayout pattern="%d{ABSOLUTE} %t %5p %c{1}:%L - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="org.genesys.blocks.security" level="error" />
<Logger name="org.genesys2.server" level="warn" />
<Logger name="org.hibernate.engine.jdbc" level="fatal" />
<Root level="warn">
<AppenderRef ref="console" />
</Root>
</Loggers>
</Configuration>
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment