如何解决 - 错误状态记录器未找到 Log4j 2 配置文件



使用 Intellij 我在 IDE 中运行时收到以下错误 -

错误状态记录器未找到 Log4j 2 配置文件。使用默认值 配置(仅将错误记录到控制台)或用户 以编程方式提供的配置。设置系统属性 "log4j2.debug"以显示 Log4j 2 内部初始化日志记录。看 https://logging.apache.org/log4j/2.x/manual/configuration.html 有关如何配置 Log4j 2 的说明

当我运行 jar 文件时 -

trevor@trevor-联想-YOGA-510-14AST:~/体面/大师5/出/神器/Master5_main_jar$ java -jar Master5.main.jar线程"main"中的异常 java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory at BlockgetConnection。(BlockgetConnection.java:12) at BlockgetAccount.getAccountByName(BlockgetAccount.java:31) at BlockgetStart.main(BlockgetStart.java:8) 由以下原因引起: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory at java.net.URLClassLoader.findClass(URLClassLoader.java:382) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ...3 更多

我没有明确记录,我假设在体面的逻辑中需要以某种方式记录。我包含了我的build.gradle。我已经包含了一些用于日志记录的依赖项。对解决方案有任何想法吗?

代码(主类) -

import ch.decent.sdk.model.Account;
public class BlockgetStart {

public static void main(String[] args) {
BlockgetAccount anAccount = new BlockgetAccount();
Account myAcct = anAccount.getAccountByName("trevor3");
}
}

使用的此类的实例 -

import ch.decent.sdk.DCoreApi;
import ch.decent.sdk.crypto.Address;
import ch.decent.sdk.crypto.Credentials;
import ch.decent.sdk.model.Account;
import ch.decent.sdk.model.AssetAmount;
import ch.decent.sdk.model.Fee;
import ch.decent.sdk.model.TransactionConfirmation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
@Component
public class BlockgetAccount {
// connection to accounts
private static final Long AMOUNT_OF_DCT_REQUIRED_FOR_CREATION = 100000L;
@Autowired
private BlockgetConnection connectionExample;
@Autowired
private BlockgetLogin loginExample;
@Autowired
private BlockgetGenerateKeys generateKeys;
/**
* Example of getting any account by its name.
*
* @param accountName name of the account e.g. dw-account
* @return Account instance for given account name
*/
public Account getAccountByName(String accountName) {
connectionExample = new BlockgetConnection();
final DCoreApi dcoreApi = connectionExample.connect();
return dcoreApi
.getAccountApi()
.getByName(accountName)
.blockingGet();
}
/**
* Example of account creation with initial fee.
*
* @param newAccountName Unique account name that you wish to create.
* @return Confirmation about transaction
*/
public TransactionConfirmation createAccount(String newAccountName) {
final DCoreApi dcoreApi = connectionExample.connect();
final Credentials credentials = loginExample.login();
final Address newAccountPublicKey = generateKeys.generateKeys();
final AssetAmount dctAssetAmount = new AssetAmount(AMOUNT_OF_DCT_REQUIRED_FOR_CREATION);
final Fee initialFee = new Fee(dctAssetAmount.getAssetId(), AMOUNT_OF_DCT_REQUIRED_FOR_CREATION);
return dcoreApi.getAccountApi().create(
credentials,
newAccountName,
newAccountPublicKey,
initialFee
).blockingGet();
}
}

Build.gradle -

plugins {
id 'java'
}
group 'com.blockget.Master5'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
jar { manifest { attributes 'Main-Class': 'BlockgetStart' } }
allprojects {
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
dependencies {
// https://mvnrepository.com/artifact/org.springframework/spring-context
// https://mvnrepository.com/artifact/org.springframework/spring-context
compile group: 'org.springframework', name: 'spring-context', version: '5.1.8.RELEASE'
// https://mvnrepository.com/artifact/org.slf4j/slf4j-ap
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.26'
// https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl
compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.12.0'
testCompile group: 'junit', name: 'junit', version: '4.12'
implementation 'com.github.DECENTfoundation:DCoreKt-SDK:2.3.1'
}

我得到这个罐子故障 -

trevor@trevor联想-瑜伽-510 14AST:~/decent/Master5/out/artifacts/Master5_main_jar$ jar tf 大师5.主要.jar 元信息/清单。中频 元信息/BlockgetNftMgr.class 阻止登录.class BlockgetConnection.class BlockgetNft.class 阻止启动.class 阻止帐户.class BlockgetGenerateKeys.class

Logger 应该在这里吗?

如果将"资源"目录标记为"资源根目录"是否有帮助?

在这种情况下,您需要将log4j2.xml文件放在 src 文件夹下,以便能够在类路径中找到它。把它放在src/main/resources或src/test/resources下。

其他解决方案:使用系统属性-Dlog4j.configurationFile=file:/path/to/file/log4j2.xml

答案如下

  • 打开项目设置
    • 选择工件
    • 单击 + 添加新项目,选择 JAR,然后选择类
    • 对于 Jar 文件,选择"复制到输出目录并通过清单链接">
    • (这不是默认值)
    • 单击确定,然后单击应用

对于清单,我也没有采用默认值,而是使用了项目的根级别。

在这一切之后,罐子都在一起,一切都工作了。

最新更新