@@ -80,6 +80,94 @@ vows.describe('log4js').addBatch({
80
80
}
81
81
} ,
82
82
83
+ 'fileAppender - with rolling based on size and number of files to keep' : {
84
+ topic : function ( ) {
85
+ var watchCb ,
86
+ filesOpened = [ ] ,
87
+ filesClosed = [ ] ,
88
+ filesRenamed = [ ] ,
89
+ newFilenames = [ ] ,
90
+ existingFiles = [ 'tests.log' ] ,
91
+ log4js = require ( '../lib/log4js' ) ( {
92
+ watchFile : function ( file , options , callback ) {
93
+ assert . equal ( file , 'tests.log' ) ;
94
+ assert . deepEqual ( options , { persistent : true , interval : 30000 } ) ;
95
+ assert . isFunction ( callback ) ;
96
+ watchCb = callback ;
97
+ } ,
98
+ openSync : function ( file ) {
99
+ assert . equal ( file , 'tests.log' ) ;
100
+ filesOpened . push ( file ) ;
101
+ return file ;
102
+ } ,
103
+ statSync : function ( file ) {
104
+ if ( existingFiles . indexOf ( file ) < 0 ) {
105
+ throw new Error ( "this file doesn't exist" ) ;
106
+ } else {
107
+ return true ;
108
+ }
109
+ } ,
110
+ renameSync : function ( oldFile , newFile ) {
111
+ filesRenamed . push ( oldFile ) ;
112
+ existingFiles . push ( newFile ) ;
113
+ } ,
114
+ closeSync : function ( file ) {
115
+ //it should always be closing tests.log
116
+ assert . equal ( file , 'tests.log' ) ;
117
+ filesClosed . push ( file ) ;
118
+ }
119
+ } ) ;
120
+ var appender = log4js . fileAppender ( 'tests.log' , log4js . messagePassThroughLayout , 1024 , 2 , 30 ) ;
121
+ return [ watchCb , filesOpened , filesClosed , filesRenamed , existingFiles ] ;
122
+ } ,
123
+
124
+ 'should close current log file, rename all old ones, open new one on rollover' : function ( args ) {
125
+ var watchCb = args [ 0 ] , filesOpened = args [ 1 ] , filesClosed = args [ 2 ] , filesRenamed = args [ 3 ] , existingFiles = args [ 4 ] ;
126
+ assert . isFunction ( watchCb ) ;
127
+ //tell the watchCb that the file is below the threshold
128
+ watchCb ( { size : 891 } , { size : 0 } ) ;
129
+ //filesOpened should still be the first one.
130
+ assert . length ( filesOpened , 1 ) ;
131
+ //tell the watchCb that the file is now over the threshold
132
+ watchCb ( { size : 1053 } , { size : 891 } ) ;
133
+ //it should have closed the first log file.
134
+ assert . length ( filesClosed , 1 ) ;
135
+ //it should have renamed the previous log file
136
+ assert . length ( filesRenamed , 1 ) ;
137
+ //and we should have two files now
138
+ assert . length ( existingFiles , 2 ) ;
139
+ assert . deepEqual ( existingFiles , [ 'tests.log' , 'tests.log.1' ] ) ;
140
+ //and opened a new log file.
141
+ assert . length ( filesOpened , 2 ) ;
142
+
143
+ //now tell the watchCb that we've flipped over the threshold again
144
+ watchCb ( { size : 1025 } , { size : 123 } ) ;
145
+ //it should have closed the old file
146
+ assert . length ( filesClosed , 2 ) ;
147
+ //it should have renamed both the old log file, and the previous '.1' file
148
+ assert . length ( filesRenamed , 3 ) ;
149
+ assert . deepEqual ( filesRenamed , [ 'tests.log' , 'tests.log' , 'tests.log.1' ] ) ;
150
+ //it should have renamed 2 more file
151
+ assert . length ( existingFiles , 2 ) ;
152
+ assert . deepEqual ( existingFiles , [ 'tests.log' , 'tests.log.2' , 'tests.log.1' ] ) ;
153
+ //and opened a new log file
154
+ assert . length ( filesOpened , 3 ) ;
155
+
156
+ //tell the watchCb we've flipped again.
157
+ watchCb ( { size : 1024 } , { size : 234 } ) ;
158
+ //close the old one again.
159
+ assert . length ( filesClosed , 3 ) ;
160
+ //it should have renamed the old log file and the 2 backups, with the last one being overwritten.
161
+ assert . length ( filesRenamed , 5 ) ;
162
+ assert . deepEqual ( filesRenamed , [ 'tests.log' , 'tests.log' , 'tests.log.1' , 'tests.log' , 'tests.log.1' ] ) ;
163
+ //it should have renamed 2 more files
164
+ assert . length ( existingFiles , 5 ) ;
165
+ assert . deepEqual ( existingFiles , [ 'tests.log' , 'tests.log.2' , 'tests.log.1' , 'tests.log.2' , 'tests.log.1' ] ) ;
166
+ //and opened a new log file
167
+ assert . length ( filesOpened , 4 ) ;
168
+ }
169
+ } ,
170
+
83
171
'configure' : {
84
172
topic : function ( ) {
85
173
var messages = { } , fakeFS = {
@@ -90,7 +178,7 @@ vows.describe('log4js').addBatch({
90
178
if ( ! messages . hasOwnProperty ( file ) ) {
91
179
messages [ file ] = [ ] ;
92
180
}
93
- messages [ file ] . push ( message ) ;
181
+ messages [ file ] . push ( message ) ;
94
182
} ,
95
183
readFileSync : function ( file , encoding ) {
96
184
return require ( 'fs' ) . readFileSync ( file , encoding ) ;
0 commit comments