Skip to content

Commit 8f350a7

Browse files
committed
Initial commit
1 parent 294bb22 commit 8f350a7

32 files changed

+1363
-1
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 4
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false
13+
14+
[*.{yml,yaml}]
15+
indent_size = 2

.gitattributes

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Path-based git attributes
2+
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html
3+
4+
# Ignore all test and documentation with "export-ignore".
5+
/.editorconfig export-ignore
6+
/.gitattributes export-ignore
7+
/.gitignore export-ignore
8+
/phpcs.xml export-ignore
9+
/phpunit.xml.dist export-ignore
10+
/tests export-ignore
11+
/docker export-ignore

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
vendor
2+
report
3+
composer.lock
4+
build
5+
.idea
6+
.phpunit.result.cache
7+
storage
8+
/.php_cs.cache

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020 Kit Loong
3+
Copyright (c) 2020 Kit Loong <kitloong1008@gmail.com>
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

composer.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "kitloong/laravel-app-logger",
3+
"description": "Laravel log for your application",
4+
"keywords": ["laravel", "log", "query", "request", "performance"],
5+
"homepage": "https://github.com/kitloong/laravel-app-logger",
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Kit Loong",
10+
"email": "kitloong1008@gmail.com"
11+
}
12+
],
13+
"require": {
14+
"php": ">=7.1.3",
15+
"illuminate/support": "^5.8|^6.0|^7.0|^8.0",
16+
"spatie/laravel-http-logger": "^1.0",
17+
"ext-json": "*"
18+
},
19+
"require-dev": {
20+
"orchestra/testbench": "^3.8|^4.0|^5.0|^6.0",
21+
"squizlabs/php_codesniffer": "^3.5",
22+
"mockery/mockery": "^1.0",
23+
"friendsofphp/php-cs-fixer": "^2.17"
24+
},
25+
"autoload": {
26+
"psr-4": {
27+
"KitLoong\\AppLogger\\": "src"
28+
}
29+
},
30+
"autoload-dev": {
31+
"psr-4": {
32+
"KitLoong\\AppLogger\\Tests\\": "tests/"
33+
}
34+
},
35+
"extra": {
36+
"laravel": {
37+
"providers": [
38+
"KitLoong\\AppLogger\\AppLoggerServiceProvider"
39+
]
40+
}
41+
},
42+
"minimum-stability": "dev",
43+
"prefer-stable": true
44+
}

config/app-logger.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
return [
4+
'http' => [
5+
'enabled' => env('RUN_HTTP_LOG', true),
6+
7+
/*
8+
* The log profile which determines whether a request should be logged.
9+
* It should implement `HttpLogProfile`.
10+
*/
11+
'log_profile' => \KitLoong\AppLogger\HttpLog\LogProfile::class,
12+
13+
/*
14+
* The log writer used to write the request to a log.
15+
* It should implement `HttpLogWriter`.
16+
*/
17+
'log_writer' => \KitLoong\AppLogger\HttpLog\LogWriter::class,
18+
19+
/*
20+
* If you are using default `HttpLogProfile` provided by the package,
21+
* you could define which HTTP methods should be logged.
22+
*/
23+
'should_log' => [
24+
\Illuminate\Http\Request::METHOD_POST,
25+
\Illuminate\Http\Request::METHOD_PUT,
26+
\Illuminate\Http\Request::METHOD_PATCH,
27+
\Illuminate\Http\Request::METHOD_DELETE
28+
],
29+
30+
/*
31+
* Filter out body fields which will never be logged.
32+
*/
33+
'except' => [
34+
'password',
35+
'password_confirmation',
36+
],
37+
38+
/*
39+
* Log channel name define in config/logging.php
40+
* null value to use default channel.
41+
*/
42+
'channel' => null,
43+
],
44+
45+
'performance' => [
46+
'enabled' => env('RUN_PERFORMANCE_LOG', true),
47+
48+
/*
49+
* The log profile which determines whether a request should be logged.
50+
* It should implement `PerformanceLogProfile`.
51+
*/
52+
'log_profile' => \KitLoong\AppLogger\PerformanceLog\LogProfile::class,
53+
54+
/*
55+
* The log writer used to write the request to a log.
56+
* It should implement `PerformanceLogWriter`.
57+
*/
58+
'log_writer' => \KitLoong\AppLogger\PerformanceLog\LogWriter::class,
59+
60+
/*
61+
* If you are using default `PerformanceLogProfile` provided by the package,
62+
* you could define which HTTP methods should be logged.
63+
*/
64+
'should_log' => [
65+
\Illuminate\Http\Request::METHOD_GET,
66+
\Illuminate\Http\Request::METHOD_POST,
67+
\Illuminate\Http\Request::METHOD_PUT,
68+
\Illuminate\Http\Request::METHOD_PATCH,
69+
\Illuminate\Http\Request::METHOD_DELETE
70+
],
71+
72+
/*
73+
* Log channel name define in config/logging.php
74+
* null value to use default channel.
75+
*/
76+
'channel' => null,
77+
],
78+
79+
'query' => [
80+
'enabled' => env('RUN_QUERY_LOG', true),
81+
82+
/*
83+
* The log profile which determines whether a request should be logged.
84+
* It should implement `QueryLogProfile`.
85+
*/
86+
'log_profile' => \KitLoong\AppLogger\QueryLog\LogProfile::class,
87+
88+
/*
89+
* The log writer used to write the request to a log.
90+
* It should implement `QueryLogWriter`.
91+
*/
92+
'log_writer' => \KitLoong\AppLogger\QueryLog\LogWriter::class,
93+
94+
/*
95+
* Log channel name define in config/logging.php
96+
* null value to use default channel.
97+
*/
98+
'channel' => null,
99+
]
100+
];

phpcs.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="laravel-app-logger">
3+
<rule ref="PSR2"/>
4+
<file>src</file>
5+
<file>tests</file>
6+
<exclude-pattern>*/migrations/*</exclude-pattern>
7+
</ruleset>

phpunit.xml.dist

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
stderr="true"
7+
>
8+
<testsuites>
9+
<testsuite name="Test Suite">
10+
<directory>./tests</directory>
11+
</testsuite>
12+
</testsuites>
13+
<!-- <coverage processUncoveredFiles="true">-->
14+
<!-- <include>-->
15+
<!-- <directory suffix=".php">./src</directory>-->
16+
<!-- </include>-->
17+
<!-- </coverage>-->
18+
<filter>
19+
<whitelist>
20+
<directory suffix=".php">src/</directory>
21+
</whitelist>
22+
</filter>
23+
<logging>
24+
<log type="junit" target="build/report.junit.xml"/>
25+
<log type="coverage-html" target="build/coverage"/>
26+
<log type="coverage-text" target="build/coverage.txt"/>
27+
<log type="coverage-clover" target="build/logs/clover.xml"/>
28+
</logging>
29+
<php>
30+
<server name="DB_CONNECTION" value="sqlite"/>
31+
<server name="DB_DATABASE" value=":memory:"/>
32+
</php>
33+
</phpunit>

src/AppLoggerServiceProvider.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace KitLoong\AppLogger;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
use KitLoong\AppLogger\HttpLog\HttpLogProfile;
7+
use KitLoong\AppLogger\HttpLog\HttpLogWriter;
8+
use KitLoong\AppLogger\Middlewares\AppLogger;
9+
use KitLoong\AppLogger\PerformanceLog\PerformanceLogProfile;
10+
use KitLoong\AppLogger\PerformanceLog\PerformanceLogWriter;
11+
use KitLoong\AppLogger\QueryLog\QueryLogProfile;
12+
use KitLoong\AppLogger\QueryLog\QueryLogWriter;
13+
14+
class AppLoggerServiceProvider extends ServiceProvider
15+
{
16+
public function register()
17+
{
18+
$this->mergeConfigFrom(__DIR__.'/../config/app-logger.php', 'app-logger');
19+
20+
$this->app->singleton(HttpLogProfile::class, config('app-logger.http.log_profile'));
21+
$this->app->singleton(HttpLogWriter::class, config('app-logger.http.log_writer'));
22+
$this->app->singleton(PerformanceLogProfile::class, config('app-logger.performance.log_profile'));
23+
$this->app->singleton(PerformanceLogWriter::class, config('app-logger.performance.log_writer'));
24+
$this->app->singleton(QueryLogProfile::class, config('app-logger.query.log_profile'));
25+
$this->app->singleton(QueryLogWriter::class, config('app-logger.query.log_writer'));
26+
27+
$this->app->singleton(AppLogger::class);
28+
}
29+
30+
public function boot()
31+
{
32+
$this->publishes([
33+
__DIR__.'/../config/app-logger.php' => config_path('app-logger.php'),
34+
]);
35+
36+
if (app(QueryLogProfile::class)->shouldLog()) {
37+
app(QueryLogWriter::class)->log();
38+
}
39+
}
40+
}

src/HttpLog/HttpLogProfile.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: liow.kitloong
5+
* Date: 2021/01/08
6+
*/
7+
8+
namespace KitLoong\AppLogger\HttpLog;
9+
10+
use Illuminate\Http\Request;
11+
12+
interface HttpLogProfile
13+
{
14+
public function shouldLog(Request $request): bool;
15+
}

0 commit comments

Comments
 (0)