转自:http://www.david0446.com/?p=52
第二课:Hello Logger!
1.编写HelloLogger
The PDS supports the standard Java logging mechanisms in the java.util.logging.* package. This is a flexible and configurable logging API used by most servers written in Java. The PDS server itself uses the logger to report various internal states and events. It is highly recommended that applications use the same logging mechanisms for their reporting.
PDS应用支持标准的Java日志机制,即java.util.logging.*包中的内容。这是个灵活的,可配置的日志API,被绝大多数Java服务器所使用。PDS服务器用日志记录各种内在的状态和事件。要记住:应用程序用相同的日志机制来进行记录。
Below is a rewrite of HelloWorld that sends the “Hello World!” string to the logger rather than to standard out:
下面会重写HelloWorld,发送“Hello World”字符串到日志记录器来代替标准输出。
/*
* Copyright 2007-2009 Sun Microsystems, Inc.
*
* This file is part of Project Darkstar Server.
*
* Project Darkstar Server is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation and
* distributed hereunder to you.
*
* Project Darkstar Server is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package
com.sun.sgs.tutorial.server.lesson2;
import
java.io.Serializable;
import
java.util.Properties;
import
java.util.logging.Level;
import
java.util.logging.Logger;
import
com.sun.sgs.app.AppListener;
import
com.sun.sgs.app.ClientSession;
import
com.sun.sgs.app.ClientSessionListener;
/**
* Hello World with Logging example for the Project Darkstar Server. It logs
* {
@code
"Hello World!"} at level {
@link
Level#INFO INFO} when first started.
*/
public
class
HelloLogger
implements
AppListener,
//
to get called during
//
application startup.
Serializable
//
since all AppListeners are ManagedObjects.
{
/**
The version of the serialized form of this class.
*/
private
static
final
long
serialVersionUID
=
1L
;
/**
The {
@link
Logger} for this class.
*/
private
static
final
Logger logger
=
Logger.getLogger(HelloLogger.
class
.getName());
/**
* {
@inheritDoc
}
*
*
* Logs our well-known greeting during application startup.
*/
public
void
initialize(Properties props) {
logger.log(Level.INFO,
"
Hello World!
"
);
}
/**
* {
@inheritDoc
}
*
*
* Prevents client logins by returning {
@code
null}.
*/
public
ClientSessionListener loggedIn(ClientSession session) {
return
null
;
}
}
2.日志配置文件
The Java logging API has a concept of message severity level. By logging at Level.INFO, we are telling the system we want to log this message at the info level of severity.
Java日志API有一个信息严重等级的概念。记录Level.INFO等级,我们就是在告诉系统,我们想记录严重等级为info的信息。
The Java logger’s behavior is controlled by a logging properties file. The sgs-boot.jar file you use to run your applications uses the file sgs-logging.properties that is present in the conf directory of your PDS installation. This is accomplished by setting the java.util.logging.config.file property on the java command line of the PDS process.
Java的日志记录器的行为受一个日志属性文件的控制。你用来运行程序的sgs-boot.jar文件,使用conf目录下的sgs-logging.properties文件。