Solved

Unix Syslog Interface using JAVA

Posted on 1998-07-29
14
538 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 2
14 Comments
 

Author Comment

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

Author Comment

by:jefftk
ID: 1228966
Adjusted points to 250
0
 

Author Comment

by:jefftk
ID: 1228967
Adjusted points to 500
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 1

Accepted Solution

by:
mbelonga earned 500 total points
ID: 1228968
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
ID: 1228969
Let me give this a try...

0
 

Author Comment

by:jefftk
ID: 1228970
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
ID: 1228971
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
ID: 1228972
I may not your help after all.  I will let you know
Thanks!
0
 

Author Comment

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

Author Comment

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

Author Comment

by:jefftk
ID: 1228975
Ok,

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

where does jsyslog come from.

0
 
LVL 1

Expert Comment

by:mbelonga
ID: 1228976
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
ID: 1228977
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
ID: 1228978
Got it to work!
continuing...
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

696 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