在Eclipse Maven Project中使用sl4j配置Logback

时间:2015-11-21 14:51:07

标签: java eclipse maven slf4j logback

我有一个eclipse maven项目,我正在尝试使用logback.xml。但是logback.xml没有被选中。我已完成此link并已确认logback.xml已放入src/main/resources/&它在我的课程中。

我的logback.xml如下所示: -

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> 
    </encoder>
  </appender>
  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- Daily rollover -->
      <fileNamePattern>/tmp/SolrUpdater.%d{yyyy-MM-dd}.log</fileNamePattern>
      <!-- Keep 7 days' worth of history -->
      <maxHistory>7</maxHistory>
    </rollingPolicy>
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>
  <root>
    <level value="INFO" />
    <appender-ref ref="CONSOLE" />
    <appender-ref ref="FILE" />
  </root>
</configuration>

Maven依赖关系如下所示: -

<?xml version="1.0" encoding="UTF-8"?>
<dependencies>
  <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.13</version>
  </dependency>
  <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-jdk14</artifactId>
    <version>1.7.13</version>
  </dependency>
  <dependency>
    <groupId>org.apache.solr</groupId>
    <artifactId>solr-solrj</artifactId>
    <version>5.2.1</version>
  </dependency>
  <dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
  </dependency>
  <dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.0.13</version>
  </dependency>
  <dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
    <version>1.0.13</version>
  </dependency>
</dependencies>

我还验证了src/main/resource在eclipse中被标记为资源目录,如下所示: - enter image description here

有人能让我知道出了什么问题吗?

2 个答案:

答案 0 :(得分:1)

您不能同时使用两个日志记录后端。您应该在类路径上看到有关多个SLF4J实现的运行时警告。

radius位于您的依赖关系列表中的import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.util.Random; import javax.swing.AbstractAction; import javax.swing.JButton; import javax.swing.JFrame; import org.openstreetmap.gui.jmapviewer.Coordinate; import org.openstreetmap.gui.jmapviewer.JMapViewer; import org.openstreetmap.gui.jmapviewer.MapMarkerCircle; import org.openstreetmap.gui.jmapviewer.MapMarkerDot; import org.openstreetmap.gui.jmapviewer.Style; /** * @see http://stackoverflow.com/a/33857113/230513 */ public class London { private static final Random r = new Random(); private void display() { JFrame f = new JFrame("London"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JMapViewer map = new JMapViewer() { @Override public Dimension getPreferredSize() { return new Dimension(320, 240); } }; Coordinate london = new Coordinate(51.5072, -0.1275); map.setDisplayPosition(london, 16); MyMarker dot = new MyMarker("", london); map.addMapMarker(dot); map.addMapMarker(new MapMarkerDot("London", london)); f.add(map); f.add(new JButton(new AbstractAction("Update") { @Override public void actionPerformed(ActionEvent e) { Style style = dot.getStyle(); style.setBackColor(Color.getHSBColor(r.nextFloat(), 1f, 1f)); style.setColor(Color.red); map.repaint(); } }), BorderLayout.SOUTH); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } private static class MyMarker extends MapMarkerCircle { public MyMarker(String name, Coordinate coord) { super(null, name, coord, 12, STYLE.FIXED, getDefaultStyle()); } } public static void main(String[] args) { EventQueue.invokeLater(new London()::display); } } 之前,因此所有日志消息都会路由到slf4j-jdk14后端,而您的Logback配置根本不起作用。

删除logback-classic依赖项,并确保没有任何其他SLF4J后端作为传递依赖项。

答案 1 :(得分:0)

可能src / main / resources没有被eclipse识别为资源目录,需要一些刷新或操作才能做到这一点。

您是否检查此目录是否在项目配置中标记为资源目录?