Merge pull request #15982 from civicrm/5.20
[civicrm-core.git] / CRM / Utils / SystemLogger.php
CommitLineData
e2bef985 1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
e2bef985 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
e2bef985 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
e2bef985 11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
e2bef985 16 */
17class CRM_Utils_SystemLogger extends Psr\Log\AbstractLogger implements \Psr\Log\LoggerInterface {
6714d8d2 18
5bc392e6
EM
19 /**
20 * Logs with an arbitrary level.
21 *
5bc392e6
EM
22 * @param mixed $level
23 * @param string $message
24 * @param array $context
5bc392e6 25 */
be2fb01f 26 public function log($level, $message, array $context = []) {
22e263ad 27 if (!isset($context['hostname'])) {
414e3596 28 $context['hostname'] = CRM_Utils_System::ipAddress();
29 }
e2bef985 30 $rec = new CRM_Core_DAO_SystemLog();
be2fb01f 31 $separateFields = ['contact_id', 'hostname'];
e2bef985 32 foreach ($separateFields as $separateField) {
33 if (isset($context[$separateField])) {
34 $rec->{$separateField} = $context[$separateField];
35 unset($context[$separateField]);
36 }
37 }
38 $rec->level = $level;
39 $rec->message = $message;
40 $rec->context = json_encode($context);
41 $rec->save();
42 }
96025800 43
e2bef985 44}