[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Unix Syslog Interface using JAVA

Posted on 1998-07-29
14
Medium Priority
?
547 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 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 practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

656 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