首页 文章

Laravel Monolog Syslog指定单独的日志文件?

提问于
浏览
2

我有Laravel设置,所以Monolog记录到 syslog . 这是我的_2514316中的最新消息:

$monolog = Log::getMonolog();
$monolog->pushHandler(new Monolog\Handler\SyslogHandler('mywebsitename'));

这很有效,除了所有日志消息都被转储到 /var/log/messages 中 .

如何指定特定的日志文件而不是 messages ?我见过一些名为custom "channels"的Monolog . 这与它有关吗?

另外,如何确保此日志文件像其他日志文件一样旋转?

1 回答

  • 0

    我正在使用Laravel 4.2,在我的setLog函数中,我添加了RotatingFileHandler . 这将为您解决这两个问题 .

    此代码在app / storage / logs / import /中创建名为mttParser- .log的文件,其中 {Y-m-d} 转换为今天的日期 . RotatingFileHandler的第二个参数10设置为在删除日志文件之前保留10个版本的日志文件 .

    IntrospectionProcessor只是添加方法,类,行号,以及调用日志的位置 .

    trait LogTrait
    {
        /**
         * @var string The location within the storage path to put the log files
         */
        protected $logFileLocation = 'logs/import/';
    
        protected $logFileName = 'mttParser.log';
    
        /**
         * @var Logger
         */
        protected $log;
    
        /**
         * Returns a valid log file, re-using an existing one if possible
         *
         * @return \Monolog\Logger
         */
        public function getLog()
        {
            // Set a log file
            if (!isset( $this->log )) {
                $this->setLog();
            }
    
            // If it's been set somewhere else, re-create it
            if ('Monolog\Logger' !== get_class( $this->log )) {
                $this->setLog();
            }
    
            return $this->log;
        }
    
        /**
         * Sets the log file
         */
        public function setLog()
        {
            $this->log = new Logger( 'mttParserLog' );
            // Make sure our directory exists
            if (!File::isDirectory( storage_path( $this->logFileLocation ) )) {
                File::makeDirectory( storage_path( $this->logFileLocation ) );
            }
    
            $this->log->pushHandler( new RotatingFileHandler( storage_path( $this->logFileLocation . $this->logFileName ),
                10 ) );
            $this->log->pushProcessor( new IntrospectionProcessor() );
        }
    }
    

相关问题