• 用PrintStream标准输出流写 logger日志文件


    用标准输出流写 logger日志文件

    package com.javaSe.LogUtil;
    
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.PrintStream;
    import java.sql.Time;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    
    /*
    日志工具
    */
    public class Logger {
        /*
        记录日志的方法。
         */
        public static void log(String msg){
            try {
                // 指向一个日志文件
                PrintStream out = new PrintStream(new FileOutputStream("log.txt",true));
                // 改变输出方向
                System.setOut(out);
                // 创建日期对象
                Date nowTime = new Date();
                // 格式化日期时间
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
                String strTime = simpleDateFormat.format(nowTime);
                System.out.println(strTime + ": " + msg);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            
        }
    }

    测试类:

    package com.javaSe.LogUtil;
    
    
    public class LoggerTest01 {
        public static void main(String[] args) {
            // 测试工具类是否好用。
            Logger.log("调用了System.gc()方法,建议启动垃圾回收!");
            Logger.log("调用了UserService的doSome()方法失败,请检查代码是否报错!");
            Logger.log("用户尝试进行登录,认证失败!");
            Logger.log("我非常喜欢这个记录日志的工具哦!");
        }
    }
  • 相关阅读:
    MySQL初始化以及更改密码
    对付小白的ARP的简单介绍
    PXE批量安装CentOS7操作系统
    20不惑
    辩论会
    学习
    JAVA语言的特点
    程序流程图对新手来说很重要。
    浅谈博客、微博与轻博客的区别与联系
    要学好JAVA要注意些什么?
  • 原文地址:https://www.cnblogs.com/xlwu/p/13466164.html
Copyright © 2020-2023  润新知