Write implements io.Writer. If a write would cause the log file to be larger than MaxSize, the file is closed, renamed to include a timestamp of the current time, and a new log file is created using the original log file name. If the length of the write is greater than MaxSize, an error is returned
(p []byte)
| 133 | // current time, and a new log file is created using the original log file name. |
| 134 | // If the length of the write is greater than MaxSize, an error is returned. |
| 135 | func (l *Logger) Write(p []byte) (n int, err error) { |
| 136 | l.mu.Lock() |
| 137 | defer l.mu.Unlock() |
| 138 | |
| 139 | writeLen := int64(len(p)) |
| 140 | if writeLen > l.max() { |
| 141 | return 0, fmt.Errorf( |
| 142 | "write length %d exceeds maximum file size %d", writeLen, l.max(), |
| 143 | ) |
| 144 | } |
| 145 | |
| 146 | if l.file == nil { |
| 147 | if err = l.openExistingOrNew(len(p)); err != nil { |
| 148 | return 0, err |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if l.size+writeLen > l.max() { |
| 153 | if err := l.rotate(); err != nil { |
| 154 | return 0, err |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | n, err = l.file.Write(p) |
| 159 | l.size += int64(n) |
| 160 | |
| 161 | return n, err |
| 162 | } |
| 163 | |
| 164 | // Close implements io.Closer, and closes the current logfile. |
| 165 | func (l *Logger) Close() error { |