如何在 Java 中写入 OS 系统日志? [英] How to write to OS system log in Java?

查看:34
本文介绍了如何在 Java 中写入 OS 系统日志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mac OS 有一个名为 Console 的应用程序,其中包含记录的消息、错误和故障.我相信 Windows 等价物是事件查看器.我想在 Linux 上也有一个,但我对它一无所知,也不知道它在哪里.

Mac OS has an application called Console, which contains logged messages, errors, and faults. I believe the Windows equivalent is Event Viewer. I'd imagine there is one on Linux as well, but I don't know anything about it or where it is.

是否可以像这样从 Java 输出中获取一条消息到系统日志?我正在编写一个基于 GUI 的应用程序,所以没有从命令行运行任何东西.标准的 System.outSystem.err 在这种情况下可能没有多大用处,除非我遗漏了一些东西.

Is it possible to get a message from Java output to a system log like this? I'm writing a GUI-based application, so there is nothing running from the command line. The standard System.out or System.err probably won't be of much use in this case, unless I'm missing something.

我为我的应用程序编写了一个简单的日志服务,用于写入专用日志文件,但我想要某种故障保护,以防在尝试写入此文件时发生 I/O 错误.

I have written a simple logging service for my application that writes to a dedicated log file, but I want to have some kind of failsafe in case an I/O error occurs while attempting to write to this file.

我知道 IDE 将通过 System.outSystem.err 显示输出就好了,但这是为了最终用户遇到这样的问题.

I know the IDE will display output via System.out and System.err just fine, but this is for if the end-user encounters a problem like this.

例如:我为 Mac 上的应用程序 TextWrangler 编写了无代码语言模块".这些模块在应用程序启动时由 TW 读取,如果在处理它们时出现错误,则会记录错误并可以在 Mac 控制台应用程序中查看.

As an example: I've written "codeless language modules" for the application TextWrangler on the Mac. These modules are read by TW at application startup, and if there is an error while processing them, errors get logged and can be viewed in the Mac Console application.

推荐答案

在 Linux 上它被称为系统日志.在 Mac 上实现控制台日志记录的方法之一是使用 log4j 'org.apache.log4j.net.SyslogAppender'.

On Linux it is called as syslog. One of the ways that you can achieve logging to console on Mac will be to use log4j 'org.apache.log4j.net.SyslogAppender'.

我认为这个链接应该会给你一些启发朝这个方向.

I think this link should give you some kickstart in this direction.

这篇关于如何在 Java 中写入 OS 系统日志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆