-2

I want to write Syslog using in java program on linux system. I don't want to use any library.

Urvin Shah
  • 61
  • 1
  • 7

3 Answers3

1

Assuming that "no libraries" means no 3rd-party libraries.

One approach would be to code your own implementation of the Syslog Protocol; see RFC 5424. You could do this in pure Java.

CORRECTION - Actually, not pure Java. The syslog protocol (typically) uses UNIX Domain sockets, and there is no built-in Java library functionality for this. You would need to resort to native code, or a 3rd-party library; see UNIX Domain Socket in Java

A second approach would be to write a JNI wrapper for the syslog(3) C library methods. Under the hood, this library opens a datagram socket on a local port and (presumably) implements the Syslog Protocol. So you don't achieve much by doing it this way.

(Note that the C libraries are part of (at least) any GNU/Linux system, so this doesn't count as using a 3rd-party library. At least, not in my books ...)

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

I used the system logger with a Runtime like this:

public static void log(String TextToLog){
    Runtime r = Runtime.getRuntime();
    try{
        r.exec("logger \"Applicationname " + TextToLog + "\"");
    }
    catch(IOException e){e.printStackTrace();}
}
Josef K.
  • 81
  • 6
0

If the distribution uses systemd, you can use the following.

String logMessage = "some message";
String logIdentifier = "java-app";
String command = "echo " + logMessage + " | systemd-cat -t " + logIdentifier;
new ProcessBuilder(new String[] {"bash","-c","eval " + command}).start();

To watch in GNU/Linux's terminal, watch for your logIdentifier:

watch -n0.5 sudo journalctl -t "java-app"

falero80s
  • 368
  • 6
  • 12