- package com.citic.util.comm;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.PrintStream;
- import java.text.DateFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import com.citic.util.comm.ConfigFileUtil;
- /**
- * Java 实现类似 C/C++ 中的__FILE__,__FUNC__,__LINE__等, 主要用于日志等功能中.
- *
- * @version 1.0 2011-07-13
- * 未使用 log4j, 没月认真研究 张明伟 20170518
- */
- public class CommFun implements IConstants{
- /**
- * 打印日志
- */
- private static String filepth = null;
- private static SimpleDateFormat ft = new SimpleDateFormat("yyyyMMddHHmmSS");
- private static String[] logfiles=new String[LOGLEVEL.values().length]; //{"err","info","data","debug","all",null};
- private static FileOutputStream[] out=new FileOutputStream[LOGLEVEL.values().length];
- private static PrintStream[] ps=new PrintStream[LOGLEVEL.values().length];
- private static PrintStream stdps=new PrintStream(System.out);
- private static CommFun commfun=null,commfun1=null;
- private CommFun() {
- filepth = ConfigFileUtil.getInstance().getPathName("..");
- String day = ft.format(new Date());
- System.out.println("dt is" + day);
- File tfile = new File(filepth + "logs/");
- if (!tfile.exists()) {
- tfile.mkdir();
- }
- // FileOutputStream out= new FileOutputStream(tfile.getPath()
- // + "/systemout" + day.substring(0, 8) + ".log", true);
- // FileOutputStream errout = new FileOutputStream(tfile.getPath()
- // + "/systemerrout" + day.substring(0, 8) + ".log", true);
- // for(int i=0;i<logfiles.length;i++){
- // try {
- // String tmpstr = tfile.getPath() + File.separator +"SUPIS_" + LOGLEVEL.values()[i].name()
- // + day.substring(0, 8) + ".log";
- // //System.out.println(tmpstr);
- // out[i] = new FileOutputStream(tmpstr, true);
- // } catch (FileNotFoundException e) {
- // e.printStackTrace();
- // }
- // ps[i] = new PrintStream(out[i]);
- // }
- }
- private CommFun(int debuglevel){
- if (debuglevel == 0) {
- return;
- }
- if (debuglevel>= ALL || debuglevel <= 0) {
- debuglevel = ALL;
- }
- if (commfun == null) {
- commfun = new CommFun();
- }
- if(ps[debuglevel-1]!=null){
- return;
- }
- File tfile = new File(filepth + "logs/");
- String day = ft.format(new Date());
- try {
- String tmpstr = tfile.getPath() + File.separator + "SUPIS_"
- + LOGLEVEL.values()[debuglevel-1].name() + day.substring(0, 8)
- + ".log";
- //System.out.println(tmpstr);
- out[debuglevel-1] = new FileOutputStream(tmpstr, true);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- ps[debuglevel-1] = new PrintStream(out[debuglevel-1]);
- }
- public static void log() {
- System.setOut(stdps);
- System.out.println(getFileLineMethod());
- }
- public static void log(int debuglevel, String... parms) {
- if (debuglevel == 0) {
- return;
- }
- new CommFun(debuglevel);
- new CommFun(ALL);
- StringBuffer stb = null;
- if (parms == null || "".equals(parms)) {
- stb = new StringBuffer("");
- } else {
- stb = new StringBuffer(parms.length);
- for (int i = 0; i <parms.length; i++) {
- stb.append(parms[i] == null ? "" : parms[i]);
- }
- }
- String messge = getFileLineMethod() + "[" + _TIME_() + "]"
- + stb.toString();
- if (debuglevel>= logfiles.length) {
- debuglevel=ALL;
- }
- System.setOut(ps[debuglevel - 1]);
- System.out.println(messge);
- if (debuglevel != ALL) {
- System.setOut(ps[ALL-1]);
- System.out.println(messge);
- }
- }
- public static void log(String... parms) {
- new CommFun(ALL);
- StringBuffer stb=null;
- if(parms==null||"".equals(parms)){
- stb=new StringBuffer("");
- }else{
- stb = new StringBuffer(parms.length);
- for(int i=0;i<parms.length;i++){
- stb.append(parms[i]==null?"":parms[i]);
- }
- }
- String messge=getFileLineMethod()+ "["+_TIME_()+"]"+ stb.toString();
- System.setOut(ps[ALL-1]);
- System.out.println(messge);
- }
- /**
- * 打印日志时获取当前的程序文件名, 行号, 方法名 输出格式为:[FileName | LineNumber | MethodName]
- *
- * @return
- */
- public static String getFileLineMethod() {
- StackTraceElement traceElement = ((new Exception()).getStackTrace())[2];
- StringBuffer toStringBuffer = new StringBuffer("[")
- .append(traceElement.getFileName()).append(":")
- .append(traceElement.getLineNumber()).append(":")
- .append(traceElement.getMethodName()).append("]");
- return toStringBuffer.toString();
- }
- // 当前文件名
- public static String _FILE_() {
- StackTraceElement traceElement = ((new Exception()).getStackTrace())[2];
- return traceElement.getFileName();
- }
- // 当前方法名
- public static String _FUNC_() {
- StackTraceElement traceElement = ((new Exception()).getStackTrace())[2];
- return traceElement.getMethodName();
- }
- // 当前行号
- public static int _LINE_() {
- StackTraceElement traceElement = ((new Exception()).getStackTrace())[2];
- return traceElement.getLineNumber();
- }
- // 当前时间
- public static String _TIME_() {
- Date now = new Date();
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
- return sdf.format(now);
- }
- // 把 string 转换成 date
- public static Date stringToDate(String date, String pattan) {
- DateFormat sf1 = new SimpleDateFormat("yyyyMMdd");
- DateFormat sf2 = new SimpleDateFormat(pattan);
- String sfstr = "";
- try {
- // sfstr = sf2.format(sf1.parse(date));
- sfstr = sf2.format(sf2.parse(date));
- } catch (ParseException e) {
- e.printStackTrace();
- }
- DateFormat df = new SimpleDateFormat(pattan);
- Date d1;
- try {
- d1 = df.parse(sfstr);
- Calendar g = Calendar.getInstance();
- g.setTime(d1);
- return g.getTime();
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return null;
- }
- public static String strNow(){
- return new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
- }
- public static String strNowRand(){
- SimpleDateFormat ft = new SimpleDateFormat("yyyyMMddHHmmssSSS");
- Date dt = new Date();
- Integer i = (int) (100000000 + Math.random() * 100000000);
- return ft.format(dt) + String.format("%010d", i);
- }
- public static void log2file(String filepth){
- try {
- File tfile = new File(filepth + "logs/");
- if (!tfile.exists()) {
- tfile.mkdir();
- }
- FileOutputStream out = new FileOutputStream(tfile.getPath()
- + "/systemout" + strNow().substring(0, 8) + ".txt", true);
- FileOutputStream errout = new FileOutputStream(tfile.getPath()
- + "/systemerrout" + strNow().substring(0, 8) + ".txt", true);
- PrintStream ps = new PrintStream(out);
- PrintStream pserr = new PrintStream(errout);
- System.setOut(ps);
- System.setErr(pserr);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- }
来源: http://www.bubuko.com/infodetail-3491382.html