Odyssey
logging.i
1 <?php
2 // vim: tabstop=2 expandtab syn=php
3 
4 /*
5  * Provide some global logging routines for Odyssey.
6  */
7 
8 
9 abstract class Logger {
10 
11  /**
12  * devMode -- Is the environment currently set to be in dev mode?
13  * {true, false (default)}
14  */
15  private $devMode = false;
16 
17  function __construct($name="root", $pDevMode=false) {
18 
19  $this->name = $name;
20  $this->devMode = $pDevMode;
21  }
22 
23  abstract protected function log($level, $log);
24 
25  protected function log_format($level, ...$args) {
26  /**
27  * args can be a variable number of messages.
28  * ONLY 1
29  * then this is the only message and there is no extra formatting
30  *
31  * > 1
32  * This option will format the string, based on the printf format, which should be the first argument in that array
33  * ** NOTE using [] is short notation for declaring an array
34  * For example $logger->info("%s %s", "Print this String", "Then this string");
35  *
36  */
37  if (count($args) == 1) {
38  $logMsg = array_shift($args);
39  } else {
40  // ** First argument is the format
41  // ** Remaining arguments will be used in the vsprintf function
42  // ** However, if there aren't enough items in the array to match the printf formatting %
43  // ** then an error is thrown. Trying to catch that here. And then just print out the array elements as a string
44  // *** NOTE: When using this method a warning will be raised if you have less arguments than formats % for vsprintf
45  $fmt = array_shift($args);
46  $logMsg = vsprintf($fmt, $args);
47  }
48  // pass string to log function and return response
49  return $this->log($level, $logMsg);
50  }
51 
52  function debug(...$msg) {
53  /**
54  * (3/6/18) - Debug prints should not make it to production.
55  * The intent of debug is purely for debugging development code.
56  * If you would like to dump extra information to the log to better track down
57  * error in production then use either
58  * info or
59  * error
60  */
61  if ($this->devMode) {
62  return $this->log_format('DEBUG', ...$msg);
63  }
64  }
65 
66  function info(...$msg) {
67  return $this->log_format('INFO', ...$msg);
68  }
69 
70  function warning(...$msg) {
71  // return $this->log_format('WARNING', ...$msg);
72  return $this->notice(...$msg);
73  }
74 
75  function notice(...$msg) {
76  return $this->log_format('NOTICE', ...$msg);
77  }
78  function error(...$msg) {
79  return $this->log_format('ERROR', ...$msg);
80  }
81 }
82 
83 
84 class StandardLogger extends Logger {
85 
86  protected function log($level, $msg) {
87  $log = sprintf("[%s] [%s]: %s\n", $level, $this->name, $msg);
88  return fwrite($this->fd, $log);
89  }
90 
91  function __construct(...$args) {
92  $this->fd = fopen('php://stdout', 'w');
93  parent::__construct(...$args);
94  }
95 }
96 
97 
99 
100  protected $level_to_color = [
101  'DEBUG' => 'yellow',
102  'INFO' => 'blue',
103  'NOTICE' => 'green',
104  'ERROR' => 'red'
105  ];
106 
107  protected $color_to_vt100 = [
108  'normal' => "",
109  'yellow' => "\033[30;43m",
110  'blue' => "\033[34;47m",
111  'green' => "\33[32m",
112  'purple' => "\033[35m",
113  'red' => "\033[37;41m"
114  ];
115 
116  protected function log($level, $msg) {
117 
118  $color = $this->level_to_color[$level];
119  $vt100 = $this->color_to_vt100[$color];
120  $level = $vt100 . $level . "\033[0m";
121  return parent::log($level, $msg);
122  }
123 }
124 
125 
126 /* Guess which logger type is best based on the output device. */
127 /**
128  * @param boolean pDevMode - {true, false(default)}Set the dev mode option of the logger
129  */
130 function getLogger($name, $pDevMode=false) {
131  $testfd = fopen('php://stdout', 'w');
132  try {
133  if (posix_isatty($testfd)) {
134  return new ColorStandardLogger($name, $pDevMode);
135  } else {
136  return new StandardLogger($name, $pDevMode);
137  }
138  } finally {
139  fclose($testfd);
140  }
141 }
142 
143 ?>
Definition: logging.i:9
debug(... $msg)
Definition: logging.i:52
log_format($level,... $args)
Definition: logging.i:25
$devMode
Definition: logging.i:15