File tree Expand file tree Collapse file tree 2 files changed +43
-6
lines changed Expand file tree Collapse file tree 2 files changed +43
-6
lines changed Original file line number Diff line number Diff line change @@ -87,19 +87,33 @@ protected function addContext(array $context = [])
87
87
{
88
88
// Add session data.
89
89
if ($ session = $ this ->app ->session ->all ()) {
90
+ $ config = $ this ->logger ->extend ([]);
91
+
92
+ if (empty ($ config ['person ' ]) or ! is_array ($ config ['person ' ])) {
93
+ $ person = [];
94
+ } else {
95
+ $ person = $ config ['person ' ];
96
+ }
97
+
90
98
// Merge person context.
91
99
if (isset ($ context ['person ' ]) and is_array ($ context ['person ' ])) {
92
- $ this -> logger -> configure ([ ' person ' => $ context ['person ' ]]) ;
100
+ $ person = $ context ['person ' ];
93
101
unset($ context ['person ' ]);
102
+ } else {
103
+ if (isset ($ config ['person_fn ' ]) && is_callable ($ config ['person_fn ' ])) {
104
+ $ data = @call_user_func ($ config ['person_fn ' ]);
105
+ if (isset ($ data ['id ' ])) {
106
+ $ person = call_user_func ($ config ['person_fn ' ]);
107
+ }
108
+ }
94
109
}
95
110
96
111
// Add user session information.
97
- $ config = $ this ->logger ->extend ([]);
98
- $ person = isset ($ config ['person ' ]) ? $ config ['person ' ] : [];
99
-
100
- $ person ['session ' ] = isset ($ person ['session ' ]) ?
101
- array_merge ($ session , $ person ['session ' ]) :
112
+ if (isset ($ person ['session ' ])) {
113
+ $ person ['session ' ] = array_merge ($ session , $ person ['session ' ]);
114
+ } else {
102
115
$ person ['session ' ] = $ session ;
116
+ }
103
117
104
118
// User session id as user id if not set.
105
119
if (! isset ($ person ['id ' ])) {
Original file line number Diff line number Diff line change @@ -177,4 +177,27 @@ public function testErrorLevels3()
177
177
$ this ->app ->log ->alert ('hello ' );
178
178
$ this ->app ->log ->emergency ('hello ' );
179
179
}
180
+
181
+ public function testPersonFunctionIsCalledWhenSessionContainsAtLeastOneItem ()
182
+ {
183
+ $ this ->app ->config ->set ('services.rollbar.person_fn ' , function () {
184
+ return [
185
+ 'id ' => '123 ' ,
186
+ 'username ' => 'joebloggs ' ,
187
+ ];
188
+ });
189
+
190
+ $ logger = $ this ->app ->make ('Rollbar\RollbarLogger ' );
191
+
192
+ $ this ->app ->session ->put ('foo ' , 'bar ' );
193
+
194
+ $ this ->app ->log ->debug ('hello ' );
195
+
196
+ $ config = $ logger ->extend ([]);
197
+
198
+ $ person = $ config ['person ' ];
199
+
200
+ $ this ->assertEquals ('123 ' , $ person ['id ' ]);
201
+ $ this ->assertEquals ('joebloggs ' , $ person ['username ' ]);
202
+ }
180
203
}
You can’t perform that action at this time.
0 commit comments