golang日志模块测试
By admin
- One minute read - 30 wordspackage main
import (
"fmt"
"log"
"os"
)
func main(){
logfile,err := os.OpenFile("d:/workspace/golang/test.log",os.O\_RDWR|os.O\_CREATE|os.O_APPEND,0);
if err!=nil {
fmt.Printf("%s\r\n",err.Error());
os.Exit(-1);
}
defer logfile.Close();
logger := log.New(logfile,"\r\n",log.Ldate|log.Ltime|log.Llongfile);
logger.Println("hello");
logger.Println("oh….");
logger.Fatal("test");
logger.Fatal("test2");
}
这里打开日志文件的时候,同时使用了””os.O_APPEND”这个,这样的话,日志会一直在文件最后面打印出来.