?
Solved

Unix Syslog Interface using JAVA

Posted on 1998-07-29
14
Medium Priority
?
541 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Accepted Solution

by:
mbelonga earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

770 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