2
2
using System . Collections . Generic ;
3
3
using System . IO ;
4
4
using System . Linq ;
5
+ using Zipper ;
5
6
6
- public class Logger {
7
- private const int DEFAULT_OUTPUT_LEVEL = 3 ;
8
- private const int MAX_LOG_COUNT = 500 ;
9
- private const string LOG_OUTPUT_PATH = ".\\ logs" ;
10
- private string logPath ;
11
- private int outputLevel ;
12
- private string subProcessName ;
7
+ namespace Zipper {
8
+ public class Logger {
9
+ private const int DEFAULT_OUTPUT_LEVEL = 3 ;
10
+ private const int MAX_LOG_COUNT = 500 ;
11
+ private const string LOG_OUTPUT_PATH = ".\\ logs" ;
12
+ private string logPath ;
13
+ private int outputLevel ;
14
+ private string subProcessName ;
13
15
14
- public Logger ( string subProcessName , string logPath , int outputLevel ) {
15
- this . subProcessName = subProcessName ;
16
- this . logPath = logPath ;
17
- this . outputLevel = outputLevel ;
18
- LogRotate ( ) ;
19
- }
20
-
21
- public Logger ( string subProcessName , int outputLevel ) {
22
- this . subProcessName = subProcessName ;
23
- this . logPath = $ ".\\ logs\\ { subProcessName } .log";
24
- this . outputLevel = outputLevel ;
25
- if ( Directory . Exists ( LOG_OUTPUT_PATH ) == false ) {
26
- Directory . CreateDirectory ( LOG_OUTPUT_PATH ) ;
16
+ public Logger ( string subProcessName , string logPath , int outputLevel ) {
17
+ this . subProcessName = subProcessName ;
18
+ this . logPath = logPath ;
19
+ this . outputLevel = outputLevel ;
20
+ LogRotate ( ) ;
27
21
}
28
- LogRotate ( ) ;
29
- }
30
- public Logger ( string subProcessName , string logPath ) {
31
- this . subProcessName = subProcessName ;
32
- this . logPath = $ ".\\ logs\\ { subProcessName } .log";
33
- this . outputLevel = DEFAULT_OUTPUT_LEVEL ;
34
- if ( Directory . Exists ( LOG_OUTPUT_PATH ) == false ) {
35
- Directory . CreateDirectory ( LOG_OUTPUT_PATH ) ;
22
+ public Logger ( string subProcessName , int outputLevel ) {
23
+ this . subProcessName = subProcessName ;
24
+ this . logPath = $ ".\\ logs\\ { subProcessName } .log";
25
+ this . outputLevel = outputLevel ;
26
+ if ( Directory . Exists ( LOG_OUTPUT_PATH ) == false ) {
27
+ Directory . CreateDirectory ( LOG_OUTPUT_PATH ) ;
28
+ }
29
+ LogRotate ( ) ;
30
+ }
31
+ public Logger ( string subProcessName , string logPath ) {
32
+ this . subProcessName = subProcessName ;
33
+ this . logPath = $ ".\\ logs\\ { subProcessName } .log";
34
+ this . outputLevel = DEFAULT_OUTPUT_LEVEL ;
35
+ if ( Directory . Exists ( LOG_OUTPUT_PATH ) == false ) {
36
+ Directory . CreateDirectory ( LOG_OUTPUT_PATH ) ;
37
+ }
38
+ LogRotate ( ) ;
36
39
}
37
- LogRotate ( ) ;
38
- }
39
40
40
- public Logger ( string subProcessName ) {
41
- this . subProcessName = subProcessName ;
42
- this . logPath = $ ".\\ logs\\ { subProcessName } .log";
43
- this . outputLevel = DEFAULT_OUTPUT_LEVEL ;
44
- if ( Directory . Exists ( LOG_OUTPUT_PATH ) == false ) {
45
- Directory . CreateDirectory ( LOG_OUTPUT_PATH ) ;
41
+ public Logger ( string subProcessName ) {
42
+ this . subProcessName = subProcessName ;
43
+ this . logPath = $ ".\\ logs\\ { subProcessName } .log";
44
+ this . outputLevel = DEFAULT_OUTPUT_LEVEL ;
45
+ if ( Directory . Exists ( LOG_OUTPUT_PATH ) == false ) {
46
+ Directory . CreateDirectory ( LOG_OUTPUT_PATH ) ;
47
+ }
48
+ LogRotate ( ) ;
46
49
}
47
- LogRotate ( ) ;
48
- }
49
50
50
- private void Base ( string logLevelStr , string message ) {
51
- string logMessage = $ "{ DateTime . Now . ToString ( $ "yyyy/MM/dd-HH:mm:ss") } [{ logLevelStr } ]:[{ subProcessName } ]{ message } \n ";
52
- Console . WriteLine ( logMessage ) ;
53
- File . AppendAllText ( logPath , logMessage ) ;
54
- }
55
- public void Debug ( string message ) {
56
- if ( outputLevel >= 3 ) {
57
- Base ( "DEBUG" , message ) ;
51
+ private void Base ( string logLevelStr , string message ) {
52
+ string logMessage = $ "{ DateTime . Now . ToString ( $ "yyyy/MM/dd-HH:mm:ss") } [{ logLevelStr } ]:[{ subProcessName } ]{ message } \n ";
53
+ Console . WriteLine ( logMessage ) ;
54
+ Program . logs . Push ( logMessage ) ;
55
+ File . AppendAllText ( logPath , logMessage ) ;
58
56
}
59
- }
60
- public void Info ( string message ) {
61
- if ( outputLevel >= 2 ) {
62
- Base ( "INFO " , message ) ;
57
+ public void Debug ( string message ) {
58
+ if ( outputLevel >= 3 ) {
59
+ Base ( "DEBUG" , message ) ;
60
+ }
63
61
}
64
- }
65
- public void Warn ( string message ) {
66
- if ( outputLevel >= 1 ) {
67
- Base ( "WARN " , message ) ;
68
-
62
+ public void Info ( string message ) {
63
+ if ( outputLevel >= 2 ) {
64
+ Base ( "INFO " , message ) ;
65
+ }
69
66
}
70
- }
71
- public void Error ( string message ) {
72
- if ( outputLevel >= 0 ) {
73
- Base ( "ERROR" , message ) ;
74
-
67
+ public void Warn ( string message ) {
68
+ if ( outputLevel >= 1 ) {
69
+ Base ( "WARN " , message ) ;
70
+ }
75
71
}
76
- }
77
-
78
- public List < string > GetLogFromFile ( ) {
79
- List < string > logs = new List < string > ( ) ;
80
-
81
- try {
82
- using ( StreamReader s = new StreamReader ( this . logPath ) ) {
83
- string _logs = s . ReadToEnd ( ) ;
84
- logs = _logs . Split ( '\n ' ) . ToList ( ) ;
72
+ public void Error ( string message ) {
73
+ if ( outputLevel >= 0 ) {
74
+ Base ( "ERROR" , message ) ;
85
75
}
86
76
}
87
- catch ( FileNotFoundException ) {
88
- File . Create ( this . logPath ) . Close ( ) ;
77
+ public List < string > GetLogFromFile ( ) {
78
+ List < string > logs = new List < string > ( ) ;
79
+ try {
80
+ using ( StreamReader s = new StreamReader ( this . logPath ) ) {
81
+ string _logs = s . ReadToEnd ( ) ;
82
+ logs = _logs . Split ( '\n ' ) . ToList ( ) ;
83
+ }
84
+ }
85
+ catch ( FileNotFoundException ) {
86
+ File . Create ( this . logPath ) . Close ( ) ;
87
+ }
88
+ //logs.Reverse();
89
+ return logs ;
89
90
}
90
- //logs.Reverse();
91
- return logs ;
92
- }
93
91
94
- private void LogRotate ( ) {
95
- Info ( "LogRotate" ) ;
96
- List < string > logs = GetLogFromFile ( ) ;
97
- //log総量が1000行を超えている場合は100行削除
98
- while ( logs . Count >= MAX_LOG_COUNT ) {
99
- logs . RemoveAt ( 0 ) ;
92
+ private void LogRotate ( ) {
93
+ Debug ( "LogRotate" ) ;
94
+ List < string > logs = GetLogFromFile ( ) ;
95
+ //log総量が1000行を超えている場合は100行削除
96
+ while ( logs . Count >= MAX_LOG_COUNT ) {
97
+ logs . RemoveAt ( 0 ) ;
98
+ }
99
+ //logs.Reverse();
100
+ File . WriteAllText ( logPath , string . Join ( "\n " , logs . ToArray ( ) ) ) ;
100
101
}
101
- //logs.Reverse();
102
- File . WriteAllText ( logPath , string . Join ( "\n " , logs . ToArray ( ) ) ) ;
103
102
}
104
103
}
0 commit comments