我有一个在514端口工作的Syslog服务器(接收日志事件和存储在文件中)。 系统日志服务器运行良好,因为已经通过路由器测试,配置了Syslog服务器和端口的IP路由器,以及路由器发送日志到Syslog服务器。
但是,现在我想创建一个替换路由器的应用程序(将事件/日志发送到服务器时)。
我有这个:
Logger mylogger;
mylogger = Logger.getLogger(this.getClass().getName());
SocketHandler myhandler = null;
try {
myhandler = new SocketHandler("localhost", 514); //With Port 80 Work!
myhandler.setLevel(Level.FINEST);
mylogger.setLevel(Level.CONFIG);
mylogger.addHandler (myhandler);
mylogger.log(Level.SEVERE, "SEVERE LOG...");
mylogger.log(Level.WARNING, "WARNING LOG...");
mylogger.log(Level.INFO, "INFO LOG...");
mylogger.log(Level.CONFIG, "CONFIG LOG...");
mylogger.log(Level.FINE, "FINE LOG...");
mylogger.log(Level.FINER, "FINER LOG...");
mylogger.log(Level.FINEST, "FINEST LOG...");
myhandler.close();
}
catch (IllegalArgumentException e) { System.out.println("IllegalArgumentException:"+e.toString()); }
catch (IOException e) { System.out.println("IOException:"+e.toString()); }
catch (SecurityException e) { System.out.println("SecurityException0:"+e.toString()); }
我有这样的信息:
IOException:java.net.ConnectException: Connection refused: connect
问题在于514端口的Socket Handler。
我需要用我的真实IP Syslog服务器和端口替换“localhost” 我该怎么办?
请帮帮我。
最诚挚的问候,
贝纳尔
答案 0 :(得分:1)
如果使用UDP,则必须使用UDP而不是TCP。
要测试UDP Syslog,您必须使用以下内容:
DatagramSocket dgSocket;
DatagramPacket dgPacket;
byte[] buf;
buf = "<67>Test Message".getBytes();
try {
InetAddress addr;
dgSocket = new DatagramSocket();
byte[] ipAddr = new byte[] { (byte)192, (byte)168, (byte)1, (byte)8 };//IP of Server Log
addr = InetAddress.getByAddress(ipAddr);
//instead of bytes using strings
String sRemIP = "192.168.1.8"; //Ip of Syslog Server
addr = InetAddress.getByName(sRemIP);
int iPort = 514;//Number of Port where listening the Syslog Server
dgPacket = new DatagramPacket(buf, buf.length, addr, iPort);
dgSocket.send(dgPacket);
System.out.println("Sending to IP:"+sRemIP+" Port:"+sPort+" The Message:"+new String(buf, "UTF8"));
}
catch (SocketException e) { System.out.println("SocketException:" + e.getMessage());}
catch (SecurityException e) { System.out.println("SecurityException:" + e.getMessage());}
catch (UnknownHostException e) { System.out.println("UnknownHostException:" + e.getMessage());}
catch (IOException e) { System.out.println("IOException:" + e.getMessage());}