katzgrau / KLogger

A Simple Logging Class For PHP
http://codefury.net/projects/klogger/
980 stars 284 forks source link

How can i set the max size of a file? #99

Open GeorgeXiaojie opened 4 years ago

GeorgeXiaojie commented 4 years ago

I have a domain process, it write all log to the same file until i restart my process.

greedying commented 3 years ago

I also want to know it

yutuanbiao commented 1 year ago

change the function of class Logger: public function setLogFilePath($logDirectory) { if ($this->options['filename']) { if (strpos($this->options['filename'], '.log') !== false || strpos($this->options['filename'], '.txt') !== false) { $this->logFilePath = $logDirectory.DIRECTORY_SEPARATOR.$this->options['filename']; } else { $this->logFilePath = $logDirectory.DIRECTORY_SEPARATOR.$this->options['filename'].'.'.$this->options['extension']; } } else { $this->logFilePath = $logDirectory . DIRECTORY_SEPARATOR . $this->options['prefix'] . date('Y-m-d') . '.' . $this->options['extension']; } }

to: public function setLogFilePath($logDirectory) { if ($this->options['filename']) { if (strpos($this->options['filename'], '.log') !== false || strpos($this->options['filename'], '.txt') !== false) { $this->logFilePath = $logDirectory.DIRECTORY_SEPARATOR.$this->options['filename']; } else { $this->logFilePath = $logDirectory.DIRECTORY_SEPARATOR.$this->options['filename'].'.'.$this->options['extension']; } } else { $this->logFilePath = $logDirectory . DIRECTORY_SEPARATOR . $this->options['prefix'] . date('Y-m-d') . '.' . $this->options['extension']; $i = 1; while (file_exists($this->logFilePath) && filesize($this->logFilePath) > 5 1024 1024) { $this->logFilePath = $logDirectory . DIRECTORY_SEPARATOR . $this->options['prefix'] . date('Y-m-d') . '(' . $i . ')' . '.' . $this->options['extension']; $i++; } } }

yutuanbiao commented 1 year ago

$logger = new Katzgrau\KLogger\Logger(DIR . '/runtime/' . date('Y-m'));