java.util.logging.FileHandler
java.util.logging.FileHandler
java自带的日志功能,FileHandler可以写日志到文件系统,并且自己维护日志的增删,比c++不知道强多少
FileHandler(filename,2,2); 测试时只写2k,并只保留2个文件:
1 import java.io.IOException; 2 import java.util.logging.FileHandler; 3 import java.util.logging.Logger; // <= (1) 4 5 public class Loggertest { 6 7 private static Logger log = Logger.getLogger(Loggertest.class.getSimpleName());// <= (2) 8 public void sub() 9 { 10 String filename="D:\\test\\1.txt"; 11 FileHandler fh; 12 try { 13 fh = new FileHandler(filename,2,2); 14 log.addHandler(fh); 15 16 log.info(" 1String userDir = System.getenv(\"USERPROFILE\");");// <= (3) 17 log.info(" 2String userDir = System.getenv(\"USERPROFILE\");");// <= (3) 18 log.info(" 3String userDir = System.getenv(\"USERPROFILE\");");// <= (3) 19 log.info(" 4String userDir = System.getenv(\"USERPROFILE\");");// <= (3) 20 log.info(" 5String userDir = System.getenv(\"USERPROFILE\");");// <= (3) 21 log.info(" 6String userDir = System.getenv(\"USERPROFILE\");");// <= (3) 22 log.info(" 7String userDir = System.getenv(\"USERPROFILE\");");// <= (3) 23 log.info(" 8String userDir = System.getenv(\"USERPROFILE\");");// <= (3) 24 log.info(" 9String userDir = System.getenv(\"USERPROFILE\");");// <= (3) 25 log.info(" 10String userDir = System.getenv(\"USERPROFILE\");");// <= (3) 26 log.info(" 11String userDir = System.getenv(\"USERPROFILE\");");// <= (3) 27 log.info(" 12String userDir = System.getenv(\"USERPROFILE\");");// <= (3) 28 29 log.info(" 13String userDir = System.getenv(\"USERPROFILE\");");// <= (3) 30 log.info(" 14String userDir = System.getenv(\"USERPROFILE\");");// <= (3) 31 log.info(" 15String userDir = System.getenv(\"USERPROFILE\");");// <= (3) 32 log.info(" 16String userDir = System.getenv(\"USERPROFILE\");");// <= (3) 33 log.info(" 17String userDir = System.getenv(\"USERPROFILE\");");// <= (3) 34 log.info(" 18String userDir = System.getenv(\"USERPROFILE\");");// <= (3) 35 log.info(" 19String userDir = System.getenv(\"USERPROFILE\");");// <= (3) 36 log.info(" 20String userDir = System.getenv(\"USERPROFILE\");");// <= (3) 37 log.info(" 21String userDir = System.getenv(\"USERPROFILE\");");// <= (3) 38 log.info(" 22String userDir = System.getenv(\"USERPROFILE\");");// <= (3) 39 } catch (SecurityException e) { 40 // TODO Auto-generated catch block 41 e.printStackTrace(); 42 } catch (IOException e) { 43 // TODO Auto-generated catch block 44 e.printStackTrace(); 45 } 46 } 47 48 public static void main(String[] args) 49 { 50 Loggertest logWorld = new Loggertest(); 51 logWorld.sub(); 52 } 53 }