For any project, the log is an important tool. Logs help us understand what happens inside our project, which will also provide review and debugging information. Want to know more about logs to view Sun's website
Http://java.sun.com/j2se/1.4.0/docs/guide/util/logging/overview.html
QuickServer currently only supports Java Logging API (Java.util.Logging). 1.4 version of Java has been added to Logging. If you are still using the old version, you can install an optional implementation provided by the LumberJack library (
http://javalogging.sourceforge.net/).
Let us make a logger for our echoserver. QuickServer defaults to use the logger, but there is no processing in addition to consolehandler and setting INFO level. Therefore, whenever we are closing and client connections in Echoserver, we will see some similar information on the console.
Feb 16, 2000 10:11:25 PM Clienthandler Sendsystemmsg
Info: Closing Connection: /127.0.0.1
The above information indicates that the client closes the IP 127.0.0.1 closes the connection. The display of this message uses the SendSystemMSG () method of the ClientHalder class.
Let us see how we control the log in our project. Use Java Logging must first import java.util.logging packages in the class.
From the previous log, we found some clients to turn off the connection or drop, but how it is displayed, we don't write any commands related to the log. It will be displayed because QuickServer uses the internal logger ClientHandler class SendSystemMSG () method.
Viewing the QuickServer documentation can find sendsystemmsg (), if there is no level, it uses INFO by default. You can record an event in a ClientCommandrandler or Authenticator or any class.
Simple log
Now let's record each client IP address that connects EchoServer. Edit the echocommandhandler.java file. Add after GotConnected (ClientHandler Handler)
Handler.sendsystemmsg ("New Client:"
Handler.getsocket (). getinetaddress (). gethostaddress (),
Level.info;
Also import package import java.util.logging. *;
Compile and run, connect to echoserver, you will see that the console will display your address when the client connection is connected.
Let QuickServer use the log to the file (XML format). The following is a modified file:
01 Package EchoServer;
02
03 Import Org.quickServer.net. *;
04 Import Org.quickServer.net.server. *;
05
06 import java.io. *;
07 Import java.util.logging. *;
08
09 public class echoserver {
10 public static void main (String s []) {
11
12 string cmd = "echoserver.echocommandhandler";
13 string auth = "echoserver.echoserverquickauthenticator";
14 string data = "echoserver.echoserverPooLableData"; // poolaable
15
16 QuickServer myServer = new quickserver ();
In one
18 // setup logger to log to file
19 Logger Logger = NULL;
20 FileHandler XMLLog = NULL;
21 File log = new file ("./ log /");
22 if (! Log.canread ())
23 Log.mkdir ();
24 try {
25 logger = logger.getlogger (""); // Get root logger
26 logger.Setlevel (Level.info);
27 xmllog = New FileHandler ("log / echoServer.xml"); 28 logger.addhandler (XMLLOG);
29} catch (ioexception e) {
30 System.err.Println ("Could Not Create XMLLog FileHandler:" E);
31}
32 // set logging level to fine
33 MyServer SetConsoleLoggingLevel (Level Info);
34
35
36 myserver.setClientCommandhandler (CMD);
37 MyServer.SetAuthenThenticator (Auth);
38 MyServer.SetClientData (data);
39
40 MyServer.Setport (4123);
41 MyServer.setName ("Echo Server V 1.0");
42
43 // Store Data Needed to Be Changd by qsadminServer
44 Object [] store = new object [] {"12.00"};
45 MyServer.SetStoreObjects (Store);
46
47 // Config QsadminServer
48 MyServer.setqsadminServerPort (4124);
49 myserver.getqsadminserver (). GetServer (). SetName ("echoadmin v 1.0");
50 try {
51 // Add Command Plugin
52 myServer.getqsadminserver (). Setcommandplugin
53 "EchoServer.QsadminCommandPlugin");
54 myserver.startqsadminserver ();
55 myServer.startServer ();
56} catch (APPEXCEPTION E) {
57 System.out.println ("Error In Server:" E);
58} catch (exception e) {
59 system.out.println ("Error:" E);
60}
61}
62}
In the above code, we first check if there is a log folder (21, 22 line), if you don't create it first.
At 25 lines, we try to get a logger, 28 lines, add a new FileHandler, where we don't specify a format because the default format is XML. At 33 lines, we set the level of logs for info.
Next to modify echocommandhandler.java
01 // echocommandhandler.java
02 Package EchoServer;
03
04 Import java.net. *;
05 import java.io. *;
06 Import Org.quick Server.Net.server.clientCommandHandler;
07 import org.quickServer.net.server.clienthandler;
08 Import java.util.logging. *;
09
10 Public Class Echocommandhandler Implements ClientCommandHandler {
11
12 Public Void Gotconnected (ClientHandler Handler)
13 throws sockettimeoutException, ioException {
14 Handler.sendsystemmsg ("New Client:"
15 handler.getsocket (). Getinetaddress (). GetHostaddress (),
16 Level.info;
17 Handler.sendClientmsg ( ");
18 Handler.sendClientmsg ("| Welcome to EchoServer V 1.0 |);
19 Handler.sendClientmsg ("| NOTE: Password = UserName |");
20 handler.sendclientmsg ("| send 'quit' to exit |");
21 Handler.sendClientmsg ( );
twenty two }
23 Public Void LostConnection (ClientHandler Handler)
24 THROWS IOException {
25 Handler.sendsystemmsg ("Connection Lost:"
26 Handler.getsocket (). Getinetaddress ());
27}
28 Public Void ClosingConnection (ClientHandler Handler)
29 thrtows oException {
30 handler.sendsystemmsg ("Closing Connection:"
31 Handler.getsocket (). Getinetaddress ());
32}
33
34 Public Void HandleCommand (ClientHandler Handler, String Command) 35 THROWS SocketTimeOutexception, ioException {
36 IF (Command.equals ("Quit")) {
37 Handler.sendClientmsg ("bye ;-)");
38 Handler.CloseConnection ();
39 return;
40}
41 IF (Command.Equals ("What's Interest?")) {
42 Handler.SendClientmsg ("Interest IS:"
43 (String) Handler.getServer (). GetStoreObjects () [0]
44 "%");
Else IF (Command.equalsignorecase ("Hello")) {
46 echoserverdata data = (echoserverdata) Handler.getClientData ();
47 data.sethellocount (data.gethellocount () 1);
48 IF (data.gethellocount () == 1) {
49 Handler.sendClientmsg ("Hello" Data.getuserName ());
50} else {
51 Handler.sendClientmsg ("You Told Hello" Data.GetHellocount ()
52 "Times");
53}
Else {
55 Handler.sendClientmsg ("Echo:" Command;
56}
57}
58}
Compile and run the program, now try to connect, you will see that it will record your IP address at the same time and XML files.
Let's use the QSADMINGUI to set the log level for Finest, observe changes in log content. The log record has increased. Another way to change the log level is to modify the code, you can use Logger.Setlevel () or QUICKSERVER's method setLogGingLevel () to set the log level of all handles.
2. Advanced application of the log
When we set the log level for Finest, a lot of information will be recorded. A possible need is to separate the logs of QuickServer and applications. The following code allows you to do this.
01 Package EchoServer;
02
03 Import Org.quickServer.net. *;
04 Import Org.quickServer.net.server. *;
05
06 import java.io. *;
07 Import java.util.logging. *;
08
09 public class echoserver {
10 public static void main (String s []) {
11
12 string cmd = "echoserver.echocommandhandler";
13 string auth = "echoserver.echoserverquickauthenticator";
14 string data = "echoserver.echoserverPooLableData"; // poolaable
15
16 QuickServer myServer = new quickserver ();
In one
18 // setup logger to log to file
19 Logger Logger = NULL;
20 FileHandler XMLLog = NULL;
21 FileHandler txtLog = NULL; 22 file log = new file ("./ log /");
23 if (! Log.canread ())
24 log.mkdir ();
25 try {
26 logger = logger.getlogger ("Org.quickServer.Net"); // Get QS Logger
27 logger.setlevel (level.finest);
28 xmllog = new filehandler ("log / echoServer.xml);
29 logger.addhandler (XMLLOG);
30
31 logger = logger.getlogger ("echoserver"); // Get App Logger
32 logger.setlevel (level.finest);
33 txtLog = New FileHandler ("log / echoServer.txt");
34 txtlog.setformatter (New SimpleFormatter ());
35 logger.addhandler (txtlog);
36 myserver.setApplogger (Logger); // img: sets logger to be used for app.
37} catch (ioexception e) {
38 System.err.Println ("Could Not Create XMLLog FileHandler: E);
39}
40 // set logging level to fine
41 MyServer.SetconsoleLoggingLevel (Level.info);
42
43
44 MyServer.SetClientCommandhandler (CMD);
45 MyServer.SetAuthenThenticator (Auth);
46 MyServer.SetClientData (data);
47
48 MyServer.Setport (4123);
49 MyServer.setName ("Echo Server V 1.0");
50
51 // Store Data Needed to Be Changd by qsadminServer
52 Object [] store = new object [] {"12.00"};
53 MyServer.SetStoreObjects (Store);
54
55 // Config QsadminServer
56 MyServer.SetqsadminServerPort (4124);
57 myserver.getqsadminserver (). GetServer (). SetName ("echoadmin v 1.0");
58 try {
59 // Add Command Plugin
60 myserver.getqsadminserver (). Setcommandplugin
61 "EchoServer.qsadminCommandPlugin");
62 myserver.startqsadminserver ();
63 myserver.startServer ();
64} catch (APPEXCEPTION E) {
65 System.out.println ("Error IN Server:" E); 66} catch (Exception E) {
67 System.out.println ("Error:" E);
68}
69}
70}
The notes in the code are very clear. Compile and run, you will see the logs inside the QuickServer to record the XML files, and the application level log has been used to the TXT file we want.
note:
To minimize the number of logs in the console, use the file to record detailed logs, and reduce the level of the log. This improves the performance of the application. Avoid using system.out.println (), replace it with logging. The Sendsystemmsg () method in ClientHandler is useful in recording logs.