2017-12-19 424 views
0

log4j 2を実装しようとしていますが、次のエラーがスローされ続けます。エラーStatusLogger Log4j2がロギング実装を見つけられませんでした

> ERROR StatusLogger Log4j2 could not find a logging implementation. 
> Please add log4j-core to the classpath. Using SimpleLogger to log to 
> the console... 
> ERROR LogExample This Will Be Printed On Error 
> FATAL LogExample This Will Be Printed On Fatal 

私はネット上の解決策を試しました。しかし、私のために働いていないようです。

これは私が実行しようとしているコードです。

package demo; 

import org.apache.logging.log4j.LogManager; 
import org.apache.logging.log4j.Logger; 

public class LogExample { 

    private static final Logger LOG = LogManager.getLogger(LogExample.class); 

    public static void main(String[] args) { 

     LOG.debug("This Will Be Printed On Debug"); 
     LOG.info("This Will Be Printed On Info"); 
     LOG.warn("This Will Be Printed On Warn"); 
     LOG.error("This Will Be Printed On Error"); 
     LOG.fatal("This Will Be Printed On Fatal"); 
     LOG.info("Appending string: {}.", "Hello, World"); 
    } 

} 

のpom.xmlに追加したプロジェクトとの依存関係:

enter image description here

enter image description here

すべてのヘルプは高く評価されます。ありがとう。

+0

log4j-core jarをクラスパスに追加する必要があります。私はあなたがmavenを使用していると仮定します。基本的に、あなたのコードだけを含むjarではなく、すべての依存関係を持つjarファイルを作成することでそれを行うことができます。これを行うには、maven shade pluginを見てください。 –

+0

回答があればそれを受け入れるか、別の方法で解決することができればあなた自身の投稿を検討してください。 –

+0

ご報告いただきありがとうございます。私は別の答えとして投稿した別の方法を見つけました。本当に助けに感謝します。 – Alok

答えて

1

log4j2設定ファイルのシステムプロパティを設定してエラーメッセージを解決しました。下のコードは以下の通りです。

System.setProperty("log4j.configurationFile","./path_to_the_log4j2_config_file/log4j2.xml"); 

Logger log = LogManager.getLogger(LogExample.class.getName()); 
関連する問題