Log4J似乎无法与sudo一起正常工作。

huangapple 未分类评论51阅读模式
标题翻译

Log4J doesn't look like to work with sudo

问题

我有一个Java应用程序,我想在Linux启动时运行此应用程序。问题在于Log4J似乎无法与“runuser”、“sudo”和“su”命令一起使用。
每次都会收到错误消息:

警告:不支持sun.reflect.Reflection.getCallerClass。这将影响性能。
(30/05/2020-15:16:11.825) [FATAL] 无法记录文件
在线程“main”中的异常java.lang.NullPointerException
        位于 me.inao.discordbot.util.Logger.<init>(Logger.java:28)
        位于 me.inao.discordbot.Main.loadConfig(Main.java:65)
        位于 me.inao.discordbot.Main.starter(Main.java:46)
        位于 me.inao.discordbot.Main.main(Main.java:43)

应用程序是开源的,所以这是我的完整源代码:https://github.com/onemoreplays/bufferBot
其他人也遇到过这种情况吗?这是因为我的编程习惯不好还是其他地方出现了bug?
非常感谢任何帮助。
Jakub。

英文翻译

I've got a Java application and I'm trying to run this application on Linux boot. Problem is that Log4J doesn't look like to work with commands "runuser", "sudo" and "su".
Every single time, I'm getting an error:

WARNING: sun.reflect.Reflection.getCallerClass is not supported. This will impact performance.
(30/05/2020-15:16:11.825) [FATAL] Unable to log file
Exception in thread &quot;main&quot; java.lang.NullPointerException
        at me.inao.discordbot.util.Logger.&lt;init&gt;(Logger.java:28)
        at me.inao.discordbot.Main.loadConfig(Main.java:65)
        at me.inao.discordbot.Main.starter(Main.java:46)
        at me.inao.discordbot.Main.main(Main.java:43)

App is Open-Sourced, so here is my full source: https://github.com/onemoreplays/bufferBot
Does this happen to someone else? Is that because of my bad programming habits or it's a bug somewhere else?<br><br>
Thank you for any help.<br>
Jakub.

答案1

得分: 0

Main#getApi() 方法在 Logger 构造函数中被执行。但是根据堆栈跟踪,当前的日志记录器是在 Main#loadConfig() 方法中实例化的(Main.java,第46行),在 api 字段实际被初始化之前(Main.java,第47行)。

更新:
在 Logger 构造函数中描述的问题隐藏了在 loadConfig 方法的 try..catch 块中抛出的原始异常。最可能的原始问题的原因是访问根目录下的 'config.json' 文件。但最好的方法是修复 Logger 类中的 NPE,这样您就可以看到原始异常的堆栈跟踪。

另外,将日志记录器与业务逻辑解耦可能是一个不错的主意。

英文翻译

Main#getApi() method is executed in the Logger constructor. But, according to the stacktrace, current logger is instantiated in the Main#loadConfig() method (Main.java, line 46), before api field is actually initialized (Main.java, line 47).

Update:
Described issue in Logger constructor hides an original exception, thrown in try..catch block of the loadConfig method. The most likely cause of the original issue is access to 'config.json' file under the root. But the best way is to fix NPE in the Logger class, so you will see stack trace of the original exception.

Also it could be a good idea to decouple Logger from your business logic.

huangapple
  • 本文由 发表于 2020年5月30日 21:38:54
  • 转载请务必保留本文链接:https://java.coder-hub.com/62103254.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定