MediaWiki fundraising/REL1_35
Logger.php
Go to the documentation of this file.
1<?php
32class Logger {
33
35 private static $inst = null;
36
38 private static $bucket = '';
40 private static $context = '';
42 private static $label = [ '' ];
43
44 private function __construct() {
45 }
46
55 public static function log( $message, $priority = LOG_INFO, $label = null ) {
56 if ( static::$inst == null ) {
57 static::$inst = new Logger();
58 }
59
60 static::$inst->logString( $message, $priority, $label );
61 }
62
72 public static function logEx( Exception $ex, $additionalText = '', $priority = LOG_ERR, $label = null ) {
73 if ( static::$inst == null ) {
74 static::$inst = new Logger();
75 }
76
77 $msg = [];
78 $msg[] = get_class( $ex );
79
80 if ( $additionalText != '' ) {
81 $msg[] = $additionalText;
82 }
83
84 $msg[] = '@';
85 $msg[] = $ex->getFile() . ':' . $ex->getLine();
86 $msg[] = '->';
87 $msg[] = $ex->getMessage();
88 $msg[] = '; Trace ->';
89 $msg[] = json_encode( $ex->getTrace() );
90
91 static::$inst->logString( implode( ' ', $msg ), $priority, $label );
92 }
93
101 public static function setBucket( $bucket ) {
102 static::$bucket = $bucket;
103 }
104
112 public static function setContext( $context ) {
113 static::$context = $context;
114 }
115
123 public static function pushLabel( $label ) {
124 static::$label[] = $label;
125 }
126
131 public static function popLabel() {
132 array_pop( static::$label );
133 }
134
141 private function logString( $msgText, $pri, $label = null ) {
142 global $wgFundraisingEmailUnsubscribeLogFacility;
143
144 if ( static::$context != '' ) {
145 $msg[] = '(' . static::$context . ')';
146 }
147 $msg[] = ':';
148 if ( $label != null ) {
149 $msg[] = $label;
150 $msg[] = ':';
151 } elseif ( end( static::$label ) != '' ) {
152 $msg[] = end( static::$label );
153 $msg[] = ':';
154 }
155 $msg[] = $msgText;
156
157 openlog( static::$bucket, LOG_ODELAY, $wgFundraisingEmailUnsubscribeLogFacility );
158 syslog( $pri, implode( ' ', $msg ) );
159 closelog();
160 }
161}
Methods for logging stuff to Syslog.
Definition Logger.php:32
static logEx(Exception $ex, $additionalText='', $priority=LOG_ERR, $label=null)
Log an exception to Syslog.
Definition Logger.php:72
static setContext( $context)
A string that will be added to the front of every log message regardless of the current label.
Definition Logger.php:112
static string $context
A persistent string prepended to all messages.
Definition Logger.php:40
static string $bucket
What process to tell syslog the message came from.
Definition Logger.php:38
static pushLabel( $label)
By pushing a label, you change the sub identifier string for each message logged after the string was...
Definition Logger.php:123
logString( $msgText, $pri, $label=null)
Perform the actual log operation.
Definition Logger.php:141
static log( $message, $priority=LOG_INFO, $label=null)
Log a message string to Syslog.
Definition Logger.php:55
static setBucket( $bucket)
Set the process Syslog thinks this is.
Definition Logger.php:101
static string[] $label
A stack of strings, whatever is on the top of the stack will get prepended to the message.
Definition Logger.php:42
static self $inst
The global instance of the Syslog class.
Definition Logger.php:35
static popLabel()
Go back to the last label.
Definition Logger.php:131
__construct()
Definition Logger.php:44