java dmesg



Что надо написать в Java-приложении, чтобы выводимый текст попал в /var/log/messages ?


FileOutputStream os = new FileOutputStream( "/var/log/messages" );

PrintStream ps = new PrintStream( os, true, "UTF-8" );

System.setOut( ps ) ;

mrco ★★
Ответ на: комментарий от Legioner

Log4j – это популярный инструмент для журналирования проекта Apache Project.

Помимо него существует еще и другие инструменты для журналирования, включая специальный API, встроенный в JDK 1.4.

Introduction to JUL

The java.util.logging package, which Sun introduced in 2002 with Java SDK version 1.4, came about as a result of JSR 47, Logging API Specification. JUL is extremely similar to Log4j - it more or less uses exactly the same concepts, but renames some of them. For example, appenders are "handlers," layouts are "formatters," and LoggingEvents are "LogRecords." JUL uses levels the same way that Log4J uses levels, although JUL has nine default levels instead of seven. JUL organizes loggers in a hierarchy the same way Log4j organizes its loggers, and JUL loggers inherit properties from their parent loggers in more or less the same way that Log4j loggers inherit properties from their parents. Concepts pretty much map one-to-one from Log4j to JUL; though the two libraries are different in subtle ways, any developer familiar with Log4j needs only to adjust his or her vocabulary to generally understand JUL. (q)
- библиотека поверх JDK 1.4, open source, Apache-style license

Ответ на: комментарий от swizard

 * A class with functionality to print the the system log.  This is only for UNIX.
public class SysLog {
  // Static constants.
  public final static int LOG_EMERG = 0;
  public final static int LOG_ALERT = 1;
  public final static int LOG_CRIT = 2;
  public final static int LOG_ERR = 3;
  public final static int LOG_WARNING = 4;
  public final static int LOG_NOTICE = 5;
  public final static int LOG_INFO = 6;
  public final static int LOG_DEBUG = 7;
  // Variables
  private static boolean _showPid = false;
   *  Static constructor.
  static { System.loadLibrary("jsyslog");}
   * Routine to print to the system log.
   * @param in_priority int priority, which can be any of the above stated variables,
   *  optionally logically OR'ed together.
   * @param in_message Message to send to the log.
  public native static void println(int in_priority, String in_message);
   * Sets whether the process id (PID) should be shown or not.  The default is to not
   *  show the pid.
  public static void showPid(boolean in_shouldShow){
    _showPid = in_shouldShow;
   * Test routine.
  public static void main(String[] args){
    SysLog.println(SysLog.LOG_INFO, "An info message from our java program (with pid).
    SysLog.println(SysLog.LOG_INFO, "An info message from our java program (with no pid).");

And then the C code for the native method...

#include <jni.h>        /* Standard native method stuff */
#include "SysLog.h"             /* Generated earlier */
#include <syslog.h>
 * Begin function code here.
Java_SysLog_println(JNIEnv *env, jclass cls, jint in_priority, jstring in_message){
  jfieldID fid;
  jboolean showPid;
  const char *message = (*env)->GetStringUTFChars(env, in_message, 0);
  fid = (*env)->GetStaticFieldID(env, cls, "_showPid", "Z");
  if (fid == 0){
  showPid = (*env)->GetStaticBooleanField(env, cls, fid);
  if (showPid){
    openlog("java", LOG_PID, LOG_USER);
  } else {
    openlog("java", ~LOG_PID, LOG_USER);

  syslog((int) in_priority, message);
  (*env)->ReleaseStringUTFChars(env, in_message, message);

The other thing to watch out for is to make sure that your /etc/syslog.conf file is set up to accept the priority type of syslog request from user processes.
Accepted Solution
back to top 

If you compile the C file into a library called, that is where the jsyslog came from.  Compiled it like this:
  cc -I<include dir> -I<java inc dir>/solaris -fPIC -c SysLogImp.c
  cc -shared -W1,-soname, -o SysLogImp.o

And then made sure that is in my LD_LIBRARY_PATH.

jni.h is in the standard JDK distribution.  (<jdk_dir>/include/jni.h)

Ответ на: комментарий от mrco

В java все не как у людей:

1) Log4j 2.0 не готов, а в 1.2 работа с syslog через JNI не присутствует.

2) Ссылка на protomatter - не по теме, та библиотека вообще в syslog писать не умеет.

3) Приведенный код через JNI не открывает сокеты (это плюс), но не доделан для использования с java.utils.logging

Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.