Commit 7f38fa19 by linxu

加入log模块

parent 8b2ecbc1
...@@ -34,19 +34,17 @@ ...@@ -34,19 +34,17 @@
<artifactId>javax.interceptor-api</artifactId> <artifactId>javax.interceptor-api</artifactId>
<version>1.2</version> <version>1.2</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
...@@ -94,6 +92,7 @@ ...@@ -94,6 +92,7 @@
<artifactId>dom4j</artifactId> <artifactId>dom4j</artifactId>
<version>2.1.0</version> <version>2.1.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.google.guava</groupId> <groupId>com.google.guava</groupId>
<artifactId>guava</artifactId> <artifactId>guava</artifactId>
...@@ -124,12 +123,6 @@ ...@@ -124,12 +123,6 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.30</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId> <groupId>redis.clients</groupId>
<artifactId>jedis</artifactId> <artifactId>jedis</artifactId>
<version>2.9.0</version> <version>2.9.0</version>
...@@ -137,14 +130,14 @@ ...@@ -137,14 +130,14 @@
<dependency> <dependency>
<groupId>com.keymobile.auth</groupId> <groupId>com.keymobile.auth</groupId>
<artifactId>common</artifactId> <artifactId>security</artifactId>
<version>3.0.7-beta</version> <version>3.0.8-beta</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.keymobile.auth</groupId> <groupId>com.keymobile</groupId>
<artifactId>security</artifactId> <artifactId>config</artifactId>
<version>3.0.7-beta</version> <version>1.0.1-release</version>
</dependency> </dependency>
</dependencies> </dependencies>
......
...@@ -3,9 +3,11 @@ package com.keymobile.login; ...@@ -3,9 +3,11 @@ package com.keymobile.login;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication @SpringBootApplication
@EnableDiscoveryClient @EnableDiscoveryClient
@ComponentScan(basePackages = {"com.keymobile.login", "com.keymobile.config.logging"})
public class LoginApplication { public class LoginApplication {
public static void main(String[] args) { public static void main(String[] args) {
......
package com.keymobile.login.logging;
import org.slf4j.MDC;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import javax.servlet.*;
import java.io.IOException;
@Component
public class MdcLogEnhancerFilter implements Filter {
@Override
public void destroy() {
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException
{
MDC.put("user", getUser());
MDC.put("session", getSession());
filterChain.doFilter(servletRequest, servletResponse);
}
private String getUser() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String userName = "NO_USER";
if (auth != null) {
userName = auth.getName();
}
return userName;
}
private String getSession() {
RequestAttributes attrs = RequestContextHolder.getRequestAttributes();
String session = "NO_SESSION";
if (attrs != null) {
session = attrs.getSessionId();
}
return session;
}
}
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<configuration> <configuration>
<springProperty name="spring.datasource.url" source="spring.datasource.url"/> <springProperty name="spring.redis.host" source="spring.redis.host"/>
<springProperty name="spring.datasource.username" source="spring.datasource.username"/> <springProperty name="spring.redis.port" source="spring.redis.port"/>
<springProperty name="spring.datasource.password" source="spring.datasource.password"/>
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender"> <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder> <encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %X{user} %X{session} %-5level %logger{5} - %msg%n</pattern> <pattern>%d{HH:mm:ss.SSS} [%thread] %X{user} %X{session} %-5level %logger{5} - %msg%n</pattern>
</encoder> </encoder>
</appender> </appender>
<appender name="db" class="ch.qos.logback.classic.db.DBAppender">
<connectionSource <appender name="logstash" class="com.cwbase.logback.RedisAppender">
class="ch.qos.logback.core.db.DriverManagerConnectionSource"> <host>${spring.redis.host}</host>
<driverClass>com.mysql.jdbc.Driver</driverClass> <port>${spring.redis.port}</port>
<url>${spring.datasource.url}</url> <key>logstash</key>
<user>${spring.datasource.username}</user>
<password>${spring.datasource.password}</password>
</connectionSource>
</appender> </appender>
<logger name="auth.AUDIT"> <logger name="dataModeler.AUDIT">
<appender-ref ref="db" /> <appender-ref ref="logstash" />
</logger> </logger>
<root level="INFO"> <root level="INFO">
<appender-ref ref="logstash" />
<appender-ref ref="stdout" /> <appender-ref ref="stdout" />
</root> </root>
</configuration>
</configuration>
\ No newline at end of file
Markdown is supported
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