@@ -11,8 +11,9 @@ import (
11
11
var (
12
12
bEnableLog bool = false
13
13
logchan chan string
14
- bShowInConsole bool = false
15
- arrLogType = [... ]string {"normal" , "warning" , "error" }
14
+ bShowInConsole bool = false
15
+ arrLogType = [... ]string {"normal" , "warning" , "error" }
16
+ mapLogPath map [int ]string = make (map [int ]string )
16
17
logPath string
17
18
)
18
19
@@ -23,15 +24,20 @@ func InitLog() {
23
24
bShowInConsole = config .GConfig .LogCfg .ShowInConsole
24
25
fmt .Println ("Log path from config file:" + config .GConfig .LogCfg .LogPath )
25
26
logPath = config .GConfig .LogCfg .LogPath
26
- //check if the logPath exists
27
- if err := file .FolderExists (logPath ); err != nil {
28
- if err = file .CreateFolder (logPath ); err != nil {
29
- panic (err )
30
- }
27
+ for key , val := range arrLogType {
28
+ checkAndCreateFolder (logPath + "/" + val + "/" , key )
31
29
}
32
30
go Listen4Log ()
33
31
}
34
32
33
+ func checkAndCreateFolder (path string , pos int ) {
34
+ if err := file .FolderExists (path ); err != nil {
35
+ if err = file .CreateFolder (path ); err != nil {
36
+ panic (err )
37
+ }
38
+ }
39
+ mapLogPath [pos ] = path
40
+ }
35
41
func wlog (info string ) {
36
42
logchan <- info
37
43
}
@@ -43,16 +49,17 @@ func Listen4Log() {
43
49
select {
44
50
case recvStr = <- logchan :
45
51
logType := recvStr [0 :1 ]
52
+ rawContent := recvStr [1 :]
46
53
if iType , err = strconv .Atoi (logType ); err != nil {
47
54
iType = 0
48
55
fmt .Printf ("error logType in Listen4Log [%s]\n " , logType )
49
56
}
50
57
if bShowInConsole {
51
58
fmt .Printf ("[%s][%s] %s\n " ,
52
- gtime .GetCurTime (gtime .BASIC_MILL ), arrLogType [iType ], recvStr [ 1 :] )
59
+ gtime .GetCurTime (gtime .BASIC_MILL ), arrLogType [iType ], rawContent )
53
60
}
54
- //file.SaveFile(logPath+"firstFile", "Hello World" )
55
- file .AddContent2File (logPath + "firstFile" , "Hello World " )
61
+ filePath := mapLogPath [ iType ] + gtime . GetCurDate ( gtime . STYLE1 )
62
+ file .AddContent2File (filePath , rawContent + " \n " )
56
63
}
57
64
}
58
65
0 commit comments