Skip to content

Commit 2438970

Browse files
authored
Merge 5724d17 into c421b1d
2 parents c421b1d + 5724d17 commit 2438970

File tree

13 files changed

+69
-10
lines changed

13 files changed

+69
-10
lines changed

src/PhpJsonLogger/JsonFormatter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function format(array $record)
1919
$formattedRecord = [
2020
'log_level' => $record['level_name'],
2121
'message' => $record['message'],
22+
'channel' => $record['channel'],
2223
'trace_id' => $record['extra']['trace_id'],
2324
'file' => $record['extra']['file'],
2425
'line' => $record['extra']['line'],

src/PhpJsonLogger/Logger.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ class Logger
2020
*/
2121
private $traceId;
2222

23+
/**
24+
* @var string
25+
* @see \Monolog\Logger::$name
26+
*/
27+
private $channel;
28+
2329
/**
2430
* @var int
2531
*/
@@ -56,6 +62,7 @@ public function __construct(LoggerBuilder $builder)
5662
{
5763
$this->createdTime = microtime(true);
5864
$this->traceId = $builder->getTraceId();
65+
$this->channel = $builder->getChannel();
5966
$this->generateTraceIdIfNeeded();
6067
$this->logFileName = $builder->getFileName();
6168
$this->logLevel = $builder->getLogLevel();
@@ -83,9 +90,8 @@ public function __construct(LoggerBuilder $builder)
8390
return $record;
8491
};
8592

86-
// TODO The channel should be configurable from outside
8793
$this->monologInstance = new MonoLogger(
88-
'PhpJsonLogger',
94+
$this->getChannel(),
8995
[$rotating],
9096
[$introspection, $extraRecords]
9197
);
@@ -171,6 +177,14 @@ public function getTraceId(): string
171177
return $this->traceId;
172178
}
173179

180+
/**
181+
* @return string
182+
*/
183+
public function getChannel(): string
184+
{
185+
return $this->channel;
186+
}
187+
174188
/**
175189
* @return int
176190
*/

src/PhpJsonLogger/LoggerBuilder.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ class LoggerBuilder
5858
*/
5959
const EMERGENCY = 600;
6060

61+
/**
62+
* @see \Monolog\Logger::$name
63+
*/
64+
const DEFAULT_CHANNEL = 'PhpJsonLogger';
65+
6166
/**
6267
* @see \Monolog\Processor\IntrospectionProcessor::$skipClassesPartials
6368
*/
@@ -78,6 +83,12 @@ class LoggerBuilder
7883
*/
7984
private $traceId;
8085

86+
/**
87+
* @var string
88+
* @see \Monolog\Logger::$name
89+
*/
90+
private $channel;
91+
8192
/**
8293
* @var int
8394
*/
@@ -114,8 +125,9 @@ class LoggerBuilder
114125
public function __construct(string $traceId = '')
115126
{
116127
$this->traceId = $traceId;
117-
$this->logLevel = self::INFO;
118-
$this->fileName = '/tmp/php-json-logger.log';
128+
$this->setChannel(self::DEFAULT_CHANNEL);
129+
$this->setLogLevel(self::INFO);
130+
$this->setFileName('/tmp/php-json-logger.log');
119131
$this->setSkipClassesPartials(self::DEFAULT_SKIP_CLASSES_PARTIALS);
120132
$this->setSkipStackFramesCount(self::DEFAULT_SKIP_STACK_FRAMES_COUNT);
121133
$this->setMaxFiles(self::DEFAULT_MAX_FILES);
@@ -137,6 +149,22 @@ public function setTraceId(string $traceId)
137149
$this->traceId = $traceId;
138150
}
139151

152+
/**
153+
* @return string
154+
*/
155+
public function getChannel(): string
156+
{
157+
return $this->channel;
158+
}
159+
160+
/**
161+
* @param string $channel
162+
*/
163+
public function setChannel(string $channel)
164+
{
165+
$this->channel = $channel;
166+
}
167+
140168
/**
141169
* @return int
142170
*/

tests/ExtendedMonologTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public function outputInfoLog()
9494
$expectedLog = [
9595
'log_level' => 'INFO',
9696
'message' => 'outputInfoLogTest',
97+
'channel' => 'ExtendedMonolog',
9798
'trace_id' => 'ExtendedMonologTestTraceId',
9899
'file' => __FILE__,
99100
'line' => 85,
@@ -136,9 +137,10 @@ public function outputErrorLog()
136137
$expectedLog = [
137138
'log_level' => 'ERROR',
138139
'message' => get_class($exception),
140+
'channel' => 'ExtendedMonolog',
139141
'trace_id' => 'ExtendedMonologTestTraceId',
140142
'file' => __FILE__,
141-
'line' => 126,
143+
'line' => 127,
142144
'context' => $context,
143145
'remote_ip_address' => '127.0.0.1',
144146
'user_agent' => 'unknown',

tests/Logger/AlertTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public function outputAlertLog()
6262
$expectedLog = [
6363
'log_level' => 'ALERT',
6464
'message' => 'ErrorException',
65+
'channel' => 'PhpJsonLogger',
6566
'trace_id' => $logger->getTraceId(),
6667
'file' => __FILE__,
6768
'line' => 53,

tests/Logger/CriticalTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public function outputCriticalLog()
6262
$expectedLog = [
6363
'log_level' => 'CRITICAL',
6464
'message' => 'ErrorException',
65+
'channel' => 'PhpJsonLogger',
6566
'trace_id' => $logger->getTraceId(),
6667
'file' => __FILE__,
6768
'line' => 53,

tests/Logger/DebugTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public function outputDebugLog()
6161
$expectedLog = [
6262
'log_level' => 'DEBUG',
6363
'message' => '🐶',
64+
'channel' => 'PhpJsonLogger',
6465
'trace_id' => $logger->getTraceId(),
6566
'file' => __FILE__,
6667
'line' => 52,

tests/Logger/EmergencyTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function outputEmergencyLog()
6363
$expectedLog = [
6464
'log_level' => 'EMERGENCY',
6565
'message' => 'ErrorException',
66+
'channel' => 'PhpJsonLogger',
6667
'trace_id' => $logger->getTraceId(),
6768
'file' => __FILE__,
6869
'line' => 54,

tests/Logger/ErrorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public function outputErrorLog()
6262
$expectedLog = [
6363
'log_level' => 'ERROR',
6464
'message' => 'Exception',
65+
'channel' => 'PhpJsonLogger',
6566
'trace_id' => $logger->getTraceId(),
6667
'file' => __FILE__,
6768
'line' => 53,

tests/Logger/InfoTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public function outputInfoLog()
6666
$expectedLog = [
6767
'log_level' => 'INFO',
6868
'message' => '🐱',
69+
'channel' => 'PhpJsonLogger',
6970
'trace_id' => $logger->getTraceId(),
7071
'file' => __FILE__,
7172
'line' => 57,

0 commit comments

Comments
 (0)