cari

Rumah  >  Soal Jawab  >  teks badan

php - 如何在 Laravel 中把自己记录的操作日志与系统生成的日志区分开单独放到一个文件里?

如何在 Laravel 中把自己记录的操作日志与系统生成的日志区分开单独放到一个文件里?

ringa_leeringa_lee2788 hari yang lalu1020

membalas semua(3)saya akan balas

  • 迷茫

    迷茫2017-04-11 08:59:43

    暂时自己实现了一个类如下:

    namespace App\Services;
    
    use Monolog\Logger;
    use Monolog\Handler\RotatingFileHandler;
    use Monolog\Processor\WebProcessor;
    use Monolog\Processor\MemoryUsageProcessor;
    use Monolog\Formatter\LineFormatter;
    use Request;
    use Sentry;
    use Config;
    use Auth;
    
    /**
     * UserLog
     *
     * Custom monolog logger for CMS user : DEBUG,INFO,NOTICE,WARNING,ERROR,CRITICAL,ALERT,EMERGENCY
     *
     * @author     
     */ 
    class UserLog {
    
        /**
         * write 
         * @return void
         */
        public static function debug($log)
        {
            self::write($log,Logger::DEBUG);
        }
        public static function info($log)
        {
            self::write($log,Logger::INFO);
        }
        public static function notice($log)
        {
            self::write($log,Logger::NOTICE);
        }
        public static function warning($log)
        {
            self::write($log,Logger::WARNING);
        }
        public static function error($log)
        {
            self::write($log,Logger::ERROR);
        }
        public static function critical($log)
        {
            self::write($log,Logger::CRITICAL);
        }
        public static function alert($log)
        {
            self::write($log,Logger::ALERT);
        }
        public static function emergency($log)
        {
            self::write($log,Logger::EMERGENCY);
        }
    
        private static function write($logtext='',$level=Logger::INFO)
        {
            if ("yes"==Config::get('app.userlog')) 
            {
                $log = new Logger('userlog');
                // handler init, making days separated logs
                $handler = new RotatingFileHandler(Config::get('app.userlog_path'), 0, $level);        
                // formatter, ordering log rows
                $handler->setFormatter(new LineFormatter("[%datetime%] %channel%.%level_name%: %message% %extra% %context%\n"));
                // add handler to the logger
                $log->pushHandler($handler);
                // processor, adding URI, IP address etc. to the log
                $log->pushProcessor(new WebProcessor);
                // processor, memory usage
                $log->pushProcessor(new MemoryUsageProcessor);
    
                $userinfo=" [] ";
                $user = Auth::user();
                if($user)
                {
                    $userinfo=' [USERID:'.$user->id.'] ';
                }
    
                $log->addInfo($logtext.$userinfo);        
            }
        }
    }

    用的时候,就是直接 UserLog.info("log here");
    默认会记录到storage/app下,但是要在app.php里增加两个配置项:

    'userlog'=>env('USER_LOG', 'yes'),
    
        'userlog_path'=>env('USER_LOG_PATH', storage_path().'/logs/useractivity.log'),

    balas
    0
  • 大家讲道理

    大家讲道理2017-04-11 08:59:43

    可以在基类中定义一个单独写日志的方法;
    比如我想把日志写到/data/logs/下

    protected function setLog($data){
        $logfilename=date('Y-m-d');
        file_put_contents("../data/logs/$logfilename.txt",var_export($data).'\r\n', FILE_APPEND);
    }
    

    使用的时候直接

    $this->setLog('logs...');

    balas
    0
  • 迷茫

    迷茫2017-04-11 08:59:43

    Storage::append()就可以了

    balas
    0
  • Batalbalas