转自: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文件。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述