zoukankan      html  css  js  c++  java
  • 小玩意--自定义log记录

       之前在帮TCL运维项目时,因某些原因,决定单就经销商相关业务中摒弃经典的log4j日志,改为每日自定义生成并写入相关日志,我遂写了一个util,代码如下:p.s.实现的思路很简单,仅为每次需要记录时,调取util中方法,若当日的日志文件不存在,则创建,存在,则追加log内容。

    package com.aebiz.b2b2c.baseframework.utils;
    
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    
    import com.aebiz.b2b2c.dealer.login.vo.BCustomerInfoModel;
    import com.aebiz.b2b2c.dealer.login.vo.ChannelUserModel;
    import com.atomikos.util.DateHelper;
    import com.mysql.fabric.xmlrpc.base.Data;
    //write by HDF WHEN 2016.06.28 14:20
    public class DealerlogUtils {
        
        public static void appendMethodA(File f, String content) {
            try {
                // 打开一个随机访问文件流,按读写方式
                RandomAccessFile randomFile = new RandomAccessFile(f, "rw");
                // 文件长度,字节数
                long fileLength = randomFile.length();
                //将写文件指针移到文件尾。
                randomFile.seek(fileLength);
                randomFile.writeBytes(content);
                randomFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        public static void appendMethodB(File f, String content) {
            try {
                //打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
                FileWriter writer = new FileWriter(f, true);
                writer.write(content+"
    ");
    //            fw.flush();
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        /**
         * 记录经销商操作日志
         * @param dealerlUser
         * @param customreInfo
         * @param menuUrl
         * @param menuName
         * @param terminalType
         */
        public static void dealerLogRecord( ChannelUserModel dealerlUser, BCustomerInfoModel customreInfo,String menuUrl, String menuName, String terminalType) {
            if(dealerlUser != null && customreInfo != null){
                StringBuffer content = new StringBuffer();
                content.append("opeTime:").append(DateFormatHelper.getNowTimeStr()).append(",loginName:")
                    .append(dealerlUser.getLoginName()).append(",bcustomerNo:").append(dealerlUser.getBcustomerNo())
                    .append(",bcustomerName:").append(customreInfo.getCustomerName()).append(",companyName:")
                    .append(customreInfo.getBranchCompanyName()).append(",orgName:").append(customreInfo.getOrgName())
                    .append(",opeName:").append(customreInfo.getSalesmanName()).append(",url:").append(menuUrl)
                    .append(",menuName:").append(menuName).append(",terminalType:").append(terminalType);
                dealerLogRecord(content.toString());
            }
        }
        
        public static void dealerLogRecord( String content) {
            
            try {
                // 根据系统的实际情况选择目录分隔符(windows下是,linux下是/)
                
                //此为相对路径,为:在tomcat下面创建日志文件,例如:dealerlog/dealerCZ_logger.log.2016-06-28.log
                //String separator = File.separator;
                //String directory = "dealerlog" + separator;
                
                //绝对路径,根目录下的/opt/logs/dealer
                String directory = "/opt/logs/dealer";
                
                // 以下这句的效果等同于上面两句,windows下正斜杠/和反斜杠都是可以的
                // linux下只认正斜杠,为了保证跨平台性,不建议使用反斜杠(在java程序中是转义字符,用来表示反斜杠)
                // String directory = "myDir1/myDir2";
    
                // 获取当前时间
                String currentDate = DateFormatHelper.getNowTimeStr();
                System.out.println(currentDate);
                String nowdate = currentDate.substring(0, 10);
                System.out.println(nowdate);
    
                String fileName = "dealerCZ_logger.log." + nowdate + ".log";
                // 在内存中创建一个文件对象,注意:此时还没有在硬盘对应目录下创建实实在在的文件
                File f = new File(directory, fileName);
                if (f.exists()) {
                    // 文件已经存在,输出文件的相关信息
                    System.out.println(f.getAbsolutePath());
                    System.out.println(f.getName());
                    System.out.println(f.length());
    
                } else {
                    // 先创建文件所在的目录
                    f.getParentFile().mkdirs();
                    f.createNewFile();
    
                }
                
                String contentNew=currentDate+" - "+content;
                appendMethodB(f,contentNew);
                
            } catch (Exception e) {
                System.out.println("创建新文件时出现了错误。。。");
                e.printStackTrace();
            }
        }
        
        
        
        public static void main(String[] args) {
    
            try {
    
                // 根据系统的实际情况选择目录分隔符(windows下是,linux下是/)
                
                //此为相对路径,为:在tomcat下面创建日志文件,例如:dealerlog/dealerCZ_logger.log.2016-06-28.log
                //String separator = File.separator;
                //String directory = "dealerlog" + separator;
                
                //绝对路径,根目录下的/opt/logs/dealer
                String directory = "/opt/logs/dealer";
                
                // 以下这句的效果等同于上面两句,windows下正斜杠/和反斜杠都是可以的
                // linux下只认正斜杠,为了保证跨平台性,不建议使用反斜杠(在java程序中是转义字符,用来表示反斜杠)
                // String directory = "myDir1/myDir2";
    
                // 获取当前时间
                String currentDate = DateFormatHelper.getNowTimeStr();
                System.out.println(currentDate);
                String nowdate = currentDate.substring(0, 10);
                System.out.println(nowdate);
    
                String fileName = "dealerCZ_logger.log." + nowdate + ".log";
                // 在内存中创建一个文件对象,注意:此时还没有在硬盘对应目录下创建实实在在的文件
                File f = new File(directory, fileName);
                if (f.exists()) {
                    // 文件已经存在,输出文件的相关信息
                    System.out.println(f.getAbsolutePath());
                    System.out.println(f.getName());
                    System.out.println(f.length());
    
                } else {
                    // 先创建文件所在的目录
                    f.getParentFile().mkdirs();
                    f.createNewFile();
    
                }
                
                
                String str = "Test by HDF!";
                String contentq=currentDate+" - "+str;
                appendMethodB(f,contentq);
                //appendMethodA(f,str);
                
            } catch (Exception e) {
                System.out.println("创建新文件时出现了错误。。。");
                e.printStackTrace();
            }
        }
    
        
        
    }
  • 相关阅读:
    android 自己定义ViewGroup实现可记载并呈现选择的ListView
    【iOS】网络载入图片缓存与SDWebImage
    Android eclipse导入项目后出现Unable to resolve target 'android-17'解决方法
    leetcode 258: Add Digits
    Mahout算法调用展示平台2.1
    rgocaupbnxj
    代码问题配置
    Tomcat_startup
    Tomcat_shutdown
    Windows环境下Oracle数据库的自动备份脚本自动删除30天前的备份
  • 原文地址:https://www.cnblogs.com/hedongfei/p/7498549.html
Copyright © 2011-2022 走看看