楼主的意思是不是要做一个程序,不论是那个java应用,你都可以通过JVM进行logging?

解决方案 »

  1.   

    luckyfanjian(luckyfan) ,谢谢,我刚才看了一下log4j的资料,感觉好像很复杂,你用过log4j吗?
    computersim(古今万卷无永日) ,我就是这个意思,你有没有作过类似的?
      

  2.   

    Log4j的确不错的,如果没有用过,可以看看sample。其实简单用用还是很容易上手的。
      

  3.   

    谢谢楼上诸位的回复,请问:
    Log4j是否不需要对具体的应用程序作修改,而只需要安装它来对JVM作监视就可以了?
      

  4.   

    需要对程序进行修改,就像你的System.out.println一样,把信息存为日志文件中
      

  5.   

    谢谢biggie(飞碟) ,
    根据我对你的回复的理解,log4j就是对log的输出进行重定向和管理的工具,
    把原来的程序中写成System.out.println("XXX");的语句利用log4j中提供的类和方法改写?
      

  6.   

    java.security.AccessController.checkPermission( )
    利用它也许可以通过JVM进行logging
      

  7.   

    Log4j日志管理系统简单使用说明 
       Log4j有三个主要的组件:Loggers,Appenders和Layouts,这里可简单理解为日志类别,日志要输出的地方和日志以何种形式输出。综合使用这三个组件可以轻松的记录信息的类型和级别,并可以在运行时控制日志输出的样式和位置。下面对三个组件分别进行说明:1、 Loggers   Loggers组件在此系统中被分为五个级别:DEBUG、INFO、WARN、ERROR和FATAL。这五个级别是有顺序的,DEBUG < INFO < WARN < ERROR < FATAL,明白这一点很重要,这里Log4j有一个规则:假设Loggers级别为P,如果在Loggers中发生了一个级别Q比P高,则可以启动,否则屏蔽掉。Java程序举例来说://建立Logger的一个实例,命名为“com.foo”Logger  logger = Logger.getLogger("com.foo");//设置logger的级别。通常不在程序中设置logger的级别。一般在配置文件中设置。logger.setLevel(Level.INFO);Logger barlogger = Logger.getLogger("com.foo.Bar");//下面这个请求可用,因为WARN >= INFOlogger.warn("Low fuel level.");//下面这个请求不可用,因为DEBUG < INFOlogger.debug("Starting search for nearest gas station.");//命名为“com.foo.bar”的实例barlogger会继承实例“com.foo”的级别。因此,下面这个请求可用,因为INFO >= INFObarlogger.info("Located nearest gas station.");//下面这个请求不可用,因为DEBUG < INFObarlogger.debug("Exiting gas station search"); 这里“是否可用”的意思是能否输出Logger信息。    在对Logger实例进行命名时,没有限制,可以取任意自己感兴趣的名字。一般情况下建议以类的所在位置来命名Logger实例,这是目前来讲比较有效的Logger命名方式。这样可以使得每个类建立自己的日志信息,便于管理。比如:static Logger logger = Logger.getLogger(ClientWithLog4j.class.getName());2、 Appenders   禁用与使用日志请求只是Log4j其中的一个小小的地方,Log4j日志系统允许把日志输出到不同的地方,如控制台(Console)、文件(Files)、根据天数或者文件大小产生新的文件、以流的形式发送到其它地方等等。其语法表示为:org.apache.log4j.ConsoleAppender(控制台),
    org.apache.log4j.FileAppender(文件),
    org.apache.log4j.DailyRollingFileAppender(每天产生一个日志文件),org.apache.log4j.RollingFileAppender(文件大小到达指定尺寸的时候产生一个新的文件),
    org.apache.log4j.WriterAppender(将日志信息以流格式发送到任意指定的地方)配置时使用方式为:log4j.appender.appenderName = fully.qualified.name.of.appender.classlog4j.appender.appenderName.option1 = value1…log4j.appender.appenderName.option = valueN这样就为日志的输出提供了相当大的便利。3、 Layouts   有时用户希望根据自己的喜好格式化自己的日志输出。Log4j可以在Appenders的后面附加Layouts来完成这个功能。Layouts提供了四种日志输出样式,如根据HTML样式、自由指定样式、包含日志级别与信息的样式和包含日志时间、线程、类别等信息的样式等等。其语法表示为:org.apache.log4j.HTMLLayout(以HTML表格形式布局),
    org.apache.log4j.PatternLayout(可以灵活地指定布局模式),
    org.apache.log4j.SimpleLayout(包含日志信息的级别和信息字符串),
    org.apache.log4j.TTCCLayout(包含日志产生的时间、线程、类别等等信息)配置时使用方式为:    log4j.appender.appenderName.layout =       fully.qualified.name.of.layout.class
        log4j.appender.appenderName.layout.option1 = value1
        …
        log4j.appender.appenderName.layout.option = valueN   以上是从原理方面说明Log4j的使用方法,在具体Java编程使用Log4j可以参照以下示例:
    1、 建立Logger实例:
       语法表示:public static Logger getLogger( String name)
       实际使用:static Logger logger = Logger.getLogger   (ServerWithLog4j.class.getName ()) ;
    2、 读取配置文件:
       获得了Logger的实例之后,接下来将配置Log4j使用环境:
       语法表示:
       BasicConfigurator.configure():自动快速地使用缺省Log4j环境。
       PropertyConfigurator.configure(String configFilename):读取使用Java的特性文件编写的配置文件。
       DOMConfigurator.configure(String filename):读取XML形式的配置文件。
       实际使用:PropertyConfigurator.configure("ServerWithLog4j.properties");
    3、 插入日志信息
       完成了以上连个步骤以后,下面就可以按日志的不同级别插入到你要记录日志的任何地方了。
       语法表示:
       Logger.debug(Object message);
       Logger.info(Object message);
       Logger.warn(Object message);
       Logger.error(Object message);
       实际使用:logger.info("ServerSocket before accept: " + server);   在实际编程时,要使Log4j真正在系统中运行事先还要对配置文件进行定义。定义步骤就是对Logger、Appender及Layout的分别使用,具体如下:
    1、 配置根Logger,其语法为:
       log4j.rootLogger = [ level ] , appenderName, appenderName, …
    这里level指Logger的优先级,appenderName是日志信息的输出地,可以同时指定多个输出地。如:log4j.rootLogger= INFO,A1,A2
    2、 配置日志信息输出目的地,其语法为:
       log4j.appender.appenderName = fully.qualified.name.of.appender.class
       可以指定上面所述五个目的地中的一个。
    3、 配置日志信息的格式,其语法为:
       log4j.appender.appenderName.layout = fully.qualified.name.of.layout.class
       这里上面三个步骤是对前面Log4j组件说明的一个简化;下面给出一个具体配置例子,在程序中可以参照执行:
       log4j.rootLogger=INFO,A1
       log4j.appender.A1=org.apache.log4j.ConsoleAppender
       log4j.appender.A1.layout=org.apache.log4j.PatternLayout
       log4j.appender.A1.layout.ConversionPattern=
                                           %-4r %-5p %d{yyyy-MM-dd HH:mm:ssS} %c %m%n
       这里需要说明的就是日志信息格式中几个符号所代表的含义:
       -X号: X信息输出时左对齐;
       %p: 日志信息级别
       %d{}: 日志信息产生时间
       %c: 日志信息所在地(类名)
       %m: 产生的日志具体信息
       %n: 输出日志信息换行根据上面的日志格式,某一个程序的输出结果如下:
    0    INFO  2003-06-13 13:23:46968 ClientWithLog4j Client socket: Socket[addr=localhost/127.0.0.1,port=8002,localport=2014]
    16   DEBUG 2003-06-13 13:23:46984 ClientWithLog4j Server says: 'Java server with log4j, Fri Jun 13 13:23:46 CST 2003'
    16   DEBUG 2003-06-13 13:23:46984 ClientWithLog4j GOOD
    16   DEBUG 2003-06-13 13:23:46984 ClientWithLog4j Server responds: 'Command 'HELLO' not understood.'
    16   DEBUG 2003-06-13 13:23:46984 ClientWithLog4j HELP
    16   DEBUG 2003-06-13 13:23:46984 ClientWithLog4j Server responds: 'Vocabulary: HELP QUIT'
    16   DEBUG 2003-06-13 13:23:46984 ClientWithLog4j QUIT -----------------------------
    以上摘的:)
      

  8.   

    谢谢诸位,我想继承AccessController生成一个对java程序访问系统资源,例如读写文件,访问某个端口的log,不知道行不行?
      

  9.   

    Inserting log statements into your code is a low-tech method for debugging it. It may also be the only way because debuggers are not always available or applicable. This is often the case for distributed applications.On the other hand, some people argue that log statements pollute source code and decrease legibility. (We believe that the contrary is true). In the Java language where a preprocessor is not available, log statements increase the size of the code and reduce its speed, even when logging is turned off. Given that a reasonably sized application may contain thousands of log statements, speed is of particular importance.With log4j it is possible to enable logging at runtime without modifying the application binary. The log4j package is designed so that these statements can remain in shipped code without incurring a heavy performance cost. Logging behavior can be controlled by editing a configuration file, without touching the application binary.Logging equips the developer with detailed context for application failures. On the other hand, testing provides quality assurance and confidence in the application. Logging and testing should not be confused. They are complementary. When logging is wisely used, it can prove to be an essential tool.One of the distinctive features of log4j is the notion of inheritance in loggers. Using a logger hierarchy it is possible to control which log statements are output at arbitrarily fine granularity but also great ease. This helps reduce the volume of logged output and minimize the cost of logging.The target of the log output can be a file, an OutputStream, a java.io.Writer, a remote log4j server, a remote Unix Syslog daemon, or even a NT Event logger among many other output targets.On an AMD Duron clocked at 800Mhz running JDK 1.3.1, it costs about 5 nanoseconds to determine if a logging statement should be logged or not. Actual logging is also quite fast, ranging from 21 microseconds microseconds using the SimpleLayout, 37 microseconds using the TTCCLayout. The performance of the PatternLayout is almost as good as the dedicated layouts, except that it is much more flexible.The package is being constantly improved thanks to input from users and code contributed by authors in the community.