Solved

Unix Syslog Interface using JAVA

Posted on 1998-07-29
14
525 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
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HashTable highest marks enumeration alternative 9 42
sql import cannot be resolved jsp 3 45
Java basic valueOf question 1 29
What browser will run Java? 7 48
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

809 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