Java:如何根据运行环境使用两种不同的日志系统
我开发了一个使用 apache log4j 库的应用程序/库。 现在我想在 Android 应用程序中使用该库,而通常的 log4j 库在该应用程序中不起作用。
现在我知道有一些适用于 android 的库 lis slf4j o log4j,但我的想法是使用可用的 log4j 库(然后当该库运行在计算机上时)并在 Android 设备上运行时使用 Android 日志记录工具。
可以这样做吗? 我认为解决方案应该是实现一个内部 Logger 类来选择可用的日志系统。
- 这是个好主意吗?
- 或者只选择一种日志系统更好?
- 或者也许更好的是,内部记录器类在两个单独的版本中实现,第一个用于真实 PC,第二个用于 Android。
还有:
- 如何检测运行环境? (是在 Android 上还是在计算机上?是否足以检测我是在 DalvikVM 还是 JavaVM 上?有可能吗?
任何帮助/建议都表示赞赏。
I have developed an application/library that use apache log4j library.
Now i want to use the library also into an Android application, where the usual log4j library doesn't work.
Now i know that there are some libraries lis slf4j o log4j for android, but my idea is to use where available log4j library (then when the library is running onto a computer) and use the Android logging facilities when it is running on Android devices.
Is possible to do that?
I think that the solution should be to implement an internal Logger class that select the available logging system.
- Is it a good idea?
- Or is better to choose only one logging system?
- Or maybe is better that the internal logger class is implemented in two separate versions the first for real pc and the second for Android.
And also:
- how can i detect the running environment? (Is it on Android or is it on a computer? is it sufficient to detect if i'm on a DalvikVM or a JavaVM? Is it possible?
Any help/suggestion is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 Java 日志记录。它也可在 android API 上使用。 android 上的这个 api 似乎是 android.util.Log 上的代表。
请注意,默认情况下,logcat 上仅打印 INFO 及以上消息。如果您想看到较低的日志级别,您可以在设备上 setprop (更多info):
并记住在您的库上设置 java 日志级别:
希望这会有所帮助
You can use Java Logging. It's available on android API too. This api on android seem that delegates on android.util.Log.
Note that by default only messages INFO and up are printed on logcat. In case you want to see lower log leves you can setprop on the device (more info):
And remember to set the java log level on your library:
Hope this help