Skip to content

Instantly share code, notes, and snippets.

@syrxw
Last active May 16, 2019 03:47
Show Gist options
  • Save syrxw/c0f03938fef32dbbe7d5a5b9ffccae23 to your computer and use it in GitHub Desktop.
Save syrxw/c0f03938fef32dbbe7d5a5b9ffccae23 to your computer and use it in GitHub Desktop.
laravel手动日之类
<?php
namespace App\Libs;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Carbon;
use App\Jobs\RecordSystemLog;
use App\Models\System\Log;
class Mlog
{
public function writeLog($data)
{
$log = $this->getLogModel();
$log->instance = $data['instance'];
$log->type = $data['type'];
$log->message = $data['message'];
$log->request_path = $data['request_path'];
$log->remote_addr = $data['remote_addr'];
$log->user_agent = $data['user_agent'];
$log->created_by = $data['created_by'];
$log->save();
}
public function record($message="",$type='platform',$async=true){
$data = [
"created_by" => Auth::user()?Auth::user()->username:'',
"instance" => gethostname(),
"request_path" => Request::route()?Request::route()->getActionName():'',
"remote_addr" => isset($_SERVER['HTTP_X_REAL_IP']) ? ip2long($_SERVER['HTTP_X_REAL_IP']) : null,
"user_agent" => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null,
"message" => $message,
"type" => $type
];
if($async){
RecordSystemLog::dispatch($data);
}else{
$this->writeLog($data);
}
}
private function getLogModel($date=null){
$dateNow = date('Y-m',strtotime(Carbon::now()));
$tableName = $date?'sys_logs_' . $date:'sys_logs_' . $dateNow;
// 日志按月分表
if(!Schema::hasTable($tableName)){
Schema::create($tableName, function ($table) {
$table->uuid('id')->unique();
$table->string('instance')->nullable()->index();
$table->string('type',30)->nullable()->index();
$table->text('message')->nullable();
$table->text('request_path')->nullable();
$table->integer('remote_addr')->nullable()->unsigned();
$table->string('user_agent')->nullable();
$table->uuid('created_by')->nullable()->index();
$table->timestamps();
});
}
$log = new Log();
$log->setTable($tableName);
return $log;
}
}
<?php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class Mlog extends Facade
{
protected static function getFacadeAccessor()
{
return 'mlog';
}
}
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Libs\Mlog;
class MlogServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->singleton('mlog',function(){
return new Mlog();
});
}
}
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Mlog;
class RecordSystemLog implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $data;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($data)
{
$this->data = $data;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
Mlog::writeLog($this->data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment