Skip to content

Commit 0f393ff

Browse files
committed
init project
0 parents  commit 0f393ff

File tree

8 files changed

+248
-0
lines changed

8 files changed

+248
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Kraken Group
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# laravel-xlog
2+
Extended Laravel Log Component
3+
XLog adds `User ID`, `User IP`, `Track ID` to each log
4+
5+
## Installation
6+
7+
```bash
8+
composer require tartan/laravel-xlog
9+
```
10+
11+
Add this to your app service providers :
12+
```php
13+
Tartan\Log\XLogServiceProvider::class,
14+
```
15+
16+
## Config (optional)
17+
add following keys to your project .env file
18+
19+
```ini
20+
# track id key
21+
XLOG_ADD_USERID= (default true)
22+
XLOG_TRACK_ID_KEY= (default xTrackId)
23+
```
24+
25+
26+
## Usage
27+
28+
```php
29+
use Tartan\Log\XLog; // or register XLog Facade
30+
31+
XLog::info('test message');
32+
XLog::notice('test message');
33+
XLog::warning('test message');
34+
XLog::error('test message');
35+
XLog::critical('test message');
36+
XLog::alert('test message');
37+
XLog::emergency('test message');
38+
```
39+
40+
## Pass parameters
41+
```php
42+
43+
// passing string
44+
$string = 'test'
45+
XLog::info('test message', [$string]);
46+
47+
// passing array
48+
$array = [1,2,'test',4.2];
49+
XLog::info('test message', $array);
50+
51+
```
52+
53+
54+
## Log exception
55+
56+
```php
57+
XLog::exception($e, 'error');
58+
```

composer.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "tartan/laravel-xlog",
3+
"version": "0.0.3",
4+
"description": "extended Laravel 5.x log",
5+
"keywords": ["laravel", "log", "xlog", "extended logger"],
6+
"type": "laravel-component",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Aboozar Ghaffari <Tartan>",
11+
"email": "aboozar.ghf@gmail.com"
12+
}
13+
],
14+
"autoload": {
15+
"psr-4": {
16+
"Tartan\\Log\\": "src"
17+
},
18+
"files": [
19+
"src/helper.php"
20+
]
21+
},
22+
"require": {
23+
"php": ">=5.4",
24+
"illuminate/support": "5.x.x"
25+
},
26+
"require-dev": {
27+
"phpunit/phpunit": "~4.0"
28+
}
29+
}

src/Facades/XLog.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Tartan\Log\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
/**
8+
* Class XLog
9+
* @package Tartan\XLog
10+
* @author Aboozar Ghaffari <aboozar.ghf@gmail.com>
11+
*/
12+
class XLog extends Facade
13+
{
14+
/**
15+
* Get the registered name of the component.
16+
*
17+
* @return string
18+
*/
19+
protected static function getFacadeAccessor ()
20+
{
21+
return 'Tartan\Log\XLog';
22+
}
23+
}

src/XLog.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Tartan\Log;
4+
5+
use Illuminate\Support\Facades\Auth;
6+
use Exception;
7+
8+
class XLog
9+
{
10+
private static $LOG_LEVELS = ['debug', 'info', 'notice', 'warning', 'error', 'critical', 'alert', 'emergency'];
11+
12+
public function __call($name, $arguments)
13+
{
14+
if (!in_array($name, self::$LOG_LEVELS)) {
15+
$name = 'debug';
16+
}
17+
18+
return call_user_func_array(['Illuminate\Support\Facades\Log', $name], $arguments);
19+
}
20+
21+
public static function __callStatic($name, $arguments)
22+
{
23+
if (session_status() == PHP_SESSION_NONE) {
24+
$arguments[1]['sid'] = session_id();
25+
} else {
26+
$arguments[1]['sid'] = '';
27+
}
28+
29+
$arguments[1]['uip'] = @clientIp();
30+
31+
// add user id to all logs
32+
if (env('XLOG_ADD_USERID', true)) {
33+
if (!Auth::guest()) {
34+
$arguments[1]['uid'] = 'us' . Auth::user()->id . 'er'; // user id as a tag
35+
}
36+
}
37+
$trackIdKey = env('XLOG_TRACK_ID_KEY', 'xTrackId');
38+
39+
// get request track ID from service container
40+
if (!isset($arguments[1][$trackIdKey])) {
41+
$arguments[1][$trackIdKey] = resolve($trackIdKey);
42+
}
43+
44+
return call_user_func_array(['Illuminate\Support\Facades\Log', $name], $arguments);
45+
}
46+
47+
public static function exception(Exception $e, $level = 'error')
48+
{
49+
$arguments = [];
50+
$arguments [0] = 'exception' . $e->getMessage();
51+
$arguments [1] = [
52+
'code' => $e->getCode(),
53+
'file' => basename($e->getFile()),
54+
'line' => $e->getLine(),
55+
];
56+
57+
return call_user_func_array(['XLog', $level], $arguments);
58+
}
59+
}

src/XLogServiceProvider.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Tartan\Log;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class XLogServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* Bootstrap the application services.
11+
*/
12+
public function boot()
13+
{
14+
//
15+
}
16+
17+
/**
18+
* Register the application services.
19+
*/
20+
public function register()
21+
{
22+
$this->app->singleton(env('XLOG_TRACK_ID_KEY', 'xTrackId'), function ($app) {
23+
return substr(0, 10, sha1(uniqid('xTrackId')));
24+
});
25+
26+
$this->app->singleton('XLog', function ($app) {
27+
return new \Tartan\Log\XLog($app);
28+
});
29+
}
30+
}

src/helper.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
if (!function_exists('clientIp')) {
4+
function clientIp()
5+
{
6+
foreach (array(
7+
'HTTP_CLIENT_IP',
8+
'HTTP_X_FORWARDED_FOR',
9+
'HTTP_X_FORWARDED',
10+
'HTTP_X_CLUSTER_CLIENT_IP',
11+
'HTTP_FORWARDED_FOR',
12+
'HTTP_FORWARDED',
13+
'REMOTE_ADDR'
14+
) as $key) {
15+
if (array_key_exists($key, $_SERVER) === true) {
16+
foreach (explode(',', $_SERVER[$key]) as $ip) {
17+
$ip = trim($ip); // just to be safe
18+
if (filter_var($ip, FILTER_VALIDATE_IP,
19+
FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false) {
20+
return $ip;
21+
}
22+
}
23+
}
24+
}
25+
return '';
26+
}
27+
}

0 commit comments

Comments
 (0)