Solved

Unix Syslog Interface using JAVA

Posted on 1998-07-29
14
500 Views
Last Modified: 2008-02-01
I need help with the Syslog interface on Unix.  Does anyone have an example of code that will call "syslog" on a UNIX box for logging messages.
 I have some code, but i cannot get the PID to display.  I was hoping that there were additional classes already written to solve this problem with usng "SYSLOG" in a unix environment.

Need Help!
0
Comment
Question by:jefftk
  • 12
  • 2
14 Comments
 

Author Comment

by:jefftk
Comment Utility
I really need help with this and would appreciate any advise you can give!!!!!!!!!!!!!!!!!!!!!!!!!
0
 

Author Comment

by:jefftk
Comment Utility
Adjusted points to 250
0
 

Author Comment

by:jefftk
Comment Utility
Adjusted points to 500
0
 
LVL 1

Accepted Solution

by:
mbelonga earned 500 total points
Comment Utility
For this, I created class SysLog, looks like this:

/**
 * 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.showPid(true);
    SysLog.println(SysLog.LOG_INFO, "An info message from our java program (with pid).
");
    SysLog.showPid(false);
    SysLog.println(SysLog.LOG_INFO, "An info message from our java program (with no pi
d).");
  }
}


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.
 */
 
JNIEXPORT void JNICALL
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){
        return;
  }
 
  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.
0
 

Author Comment

by:jefftk
Comment Utility
Let me give this a try...

0
 

Author Comment

by:jefftk
Comment Utility
One more thing...

The line "static { System.loadLibrary("jsyslog");} "
where does jsyslog come from.

Also,  do you have an example of the JNI.H file ?
thanks
0
 

Author Comment

by:jefftk
Comment Utility
One more thing...

The line "static { System.loadLibrary("jsyslog");} "
where does jsyslog come from.

Also,  do you have an example of the JNI.H file ?
thanks
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:jefftk
Comment Utility
I may not your help after all.  I will let you know
Thanks!
0
 

Author Comment

by:jefftk
Comment Utility
I may not your help after all.  I will let you know
Thanks!
0
 

Author Comment

by:jefftk
Comment Utility
I may not your help after all.  I will let you know
Thanks!
0
 

Author Comment

by:jefftk
Comment Utility
Ok,

what is the line "static { System.loadLibrary("jsyslog");} "

where does jsyslog come from.

0
 
LVL 1

Expert Comment

by:mbelonga
Comment Utility
If you compile the C file into a library called libjsyslog.so, 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,libjsyslog.so.1 -o libjsyslog.so.1.0 SysLogImp.o
  cp libjsyslog.so.1.0 libjsyslog.so

And then made sure that libjsyslog.so is in my LD_LIBRARY_PATH.

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

Author Comment

by:jefftk
Comment Utility
Question:

i am trying to run javah -jni to create the header file, but i keep getting "no such class"

Any ideas ?
0
 

Author Comment

by:jefftk
Comment Utility
Got it to work!
continuing...
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now