-3

i am currently trying to log data to syslog and i am using "Runtime.getRuntime().exec" statement in java to do that. The full statement is:

Runtime.getRuntime().exec("logger " +  data.toString());

Now this works perfectly but it seems too easy and i wanted to know if directly using this has bad effects. Also if there are better ways to log data (except for log4j library) to syslog i am open to it. Thanks.

Syed Muhammad Oan
  • 687
  • 2
  • 15
  • 39

1 Answers1

2

You are spawning a new process for every single log entry, which is hideously expensive. Plus you may open yourself up to injection attacks by passing data.toString() to the command line. Do what the Log4j library probably does (it is open source if you want inspiration) and send UDP messages. That is cheap and it is also portable.

The protocol is defined in rfc5424 and rfc5426 if you want to write the networking code from scratch.

Community
  • 1
  • 1
ewramner
  • 5,810
  • 2
  • 17
  • 33
  • Actually log4j also works great. But i am stuck because whenever i log using log4j the hostname shown in log is "localhost" and just can't get around that. Any ideas? – Syed Muhammad Oan Apr 22 '19 at 06:18
  • https://stackoverflow.com/questions/7744840/how-can-i-change-localhost-localdomain-in-the-messages-written-by-log4j-to-the-l – ewramner Apr 22 '19 at 06:20
  • thanks a lot, it worked. I need to work a little more on my google searching. Need to use the right keywords lol. – Syed Muhammad Oan Apr 22 '19 at 06:23