Skip to content

Commit

Permalink
Lesson10: Add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
RuslanSatybaev committed Feb 20, 2023
1 parent b6c995a commit 3db22fa
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ log
*.patch


/src/main/java/META-INF/MANIFEST.MF
28 changes: 27 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

Expand All @@ -15,6 +15,10 @@
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<!-- Logging -->
<logback.version>1.2.3</logback.version>
<slf4j.version>1.7.28</slf4j.version>
</properties>

<build>
Expand All @@ -34,6 +38,28 @@
</build>

<dependencies>
<!-- Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>runtime</scope>
</dependency>

<!--Web-->
<dependency>
<groupId>javax.servlet</groupId>
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/ru/javawebinar/topjava/web/UserServlet.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package ru.javawebinar.topjava.web;

import org.slf4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static org.slf4j.LoggerFactory.getLogger;

public class UserServlet extends HttpServlet {
private static final Logger LOG = getLogger(UserServlet.class);

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
LOG.debug("redirect to userList");
response.sendRedirect("/users.jsp");
// request.getRequestDispatcher("/users.jsp").forward(request, response);
// request.getRequestDispatcher("/users.jsp").forward(request, response);
}
}
29 changes: 29 additions & 0 deletions src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds">

<!-- To enable JMX Management -->
<jmxConfigurator/>

<appender name="file" class="ch.qos.logback.core.FileAppender">
<file>${TOPJAVA_ROOT}/log/topjava.log</file>

<encoder>
<charset>UTF-8</charset>
<pattern>%date %-5level %logger{0} [%file:%line] %msg%n</pattern>
</encoder>
</appender>

<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>UTF-8</charset>
<pattern>%-5level %logger{0} [%file:%line] %msg%n</pattern>
</encoder>
</appender>

<logger name="ru.javawebinar.topjava" level="debug"/>

<root level="info">
<appender-ref ref="file"/>
<appender-ref ref="console"/>
</root>
</configuration>

0 comments on commit 3db22fa

Please sign in to comment.