afb63465c0baf4a2cb3e6d85b1ea1790fa9949cf
[logging-log4php.git] / src / main / php / appenders / LoggerAppenderFirephp.php
1 <?php
2
3 /**
4 * LoggerAppenderFirephp
5 *
6 * Licensed to the Apache Software Foundation (ASF) under one or more contributor
7 * license agreements. See the NOTICE file distributed with this work for
8 * additional information regarding copyright ownership. The ASF licenses this
9 * file to you under the Apache License, Version 2.0 (the "License"); you may not
10 * use this file except in compliance with the License. You may obtain a copy of
11 * the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software distributed
14 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
15 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations under the License.
17 *
18 * PHP version 5
19 *
20 * @category Include
21 * @package LoggerAppenderFirephp
22 * @author Bruce Ingalls <Bruce.Ingalls-at-gmail-dot-com>
23 * @copyright 2012 Apache Software Foundation
24 * @license Apache License, Version 2.0
25 * @version SVN: $Id:$
26 * @link http://sourcemint.com/github.com/firephp/firephp/1:1.0.0b1rc6/-docs/Configuration/Constants
27 * @link https://github.com/Seldaek/monolog/blob/master/src/Monolog/Handler/FirePHPHandler.php
28 * @see LoggerAutoloader.php Update class list
29 * @since Feb 22, 2012
30 * @internal CodeSniffs as PEAR style, adapted to Apache. Phpmd clean.
31 * @example require_once 'FirePHP/Init.php'; //Must be declared before log4php
32 */
33
34 /**
35 * Connect Apache Log4php to Insight, the next version of FirePHP
36 * Read the link at sourcemint, to <b>define</b> 4 INSIGHT_* constants in your php
37 * config / bootstrap
38 *
39 * @category Include
40 * @package LoggerAppenderFirephp
41 * @author Bruce Ingalls <Bruce.Ingalls-at-gmail-dot-com>
42 * @copyright 2012 Apache Software Foundation
43 * @license Apache License, Version 2.0
44 * @link http://sourcemint.com/github.com/firephp/firephp/1:1.0.0b1rc6/-docs/Configuration/Constants
45 * @example define('INSIGHT_IPS', '*'); //If using firephp lib
46 * @example define('INSIGHT_AUTHKEYS', '*'); //weak security (ok for dev only)
47 * @example define('INSIGHT_PATHS', dirname(__FILE__));
48 * @example define('INSIGHT_SERVER_PATH', '/index.php');
49 */
50 class LoggerAppenderFirephp extends LoggerAppender {
51 protected $console;
52 protected $medium;
53
54 /**
55 * Default constructor
56 *
57 * @param string $name Default ''
58 */
59 public function __construct($name = '') {
60 parent::__construct($name);
61 }
62
63 /**
64 * Enable
65 *
66 * @return void
67 */
68 public function activateOptions() {
69 $this->closed = false;
70 }
71
72 /**
73 * Write event object to Log. Defaults to INFO level
74 *
75 * @param LoggerLoggingEvent $event Includes level & message
76 *
77 * @return void
78 */
79 public function append(LoggerLoggingEvent $event) {
80 $console = $this->getConsole();
81 if (null === $console) {
82 $this->warn('FirePHP is not installed correctly.');
83 }
84
85 $msg = $this->getLayout()->format($event);
86
87 switch ($this->getLogLevel($event)) {
88 case 'debug':
89 $console->trace($msg); //includes backtrace
90 break;
91 case 'warn':
92 $console->debug($msg);
93 break;
94 case 'error':
95 $console->warn($msg);
96 break;
97 case 'fatal':
98 $console->error($msg);
99 break;
100 default:
101 $console->info($msg);
102 }
103 }
104
105 private function getLogLevel(LoggerLoggingEvent $event) {
106 return strtolower($event->getLevel()->toString());
107 }
108
109 /**
110 * Disable
111 *
112 * @return void
113 */
114 public function close() {
115 $this->closed = true;
116 }
117
118 public function __destruct() {
119 $this->close();
120 }
121
122 /**
123 * Get firephp display object
124 *
125 * @return object null, if FirePHP is unavailable
126 */
127 private function getConsole() {
128 if (isset($this->console)) {
129 return $this->console;
130 }
131
132 if (method_exists('FirePHP', 'to')) {
133 $inspector = FirePHP::to($this->getMedium());
134
135 return $inspector->console();
136 }
137
138 return null;
139 }
140
141 /**
142 * Get medium, typically 'page'
143 *
144 * @return string
145 */
146 public function getMedium() {
147 return $this->medium;
148 }
149
150 /**
151 * Sets medium. Defaults to 'page' for firebug console
152 *
153 * @param string $medium
154 *
155 * @return void
156 */
157 public function setMedium($medium='page') {
158 $this->setString('medium', $medium);
159 }
160
161 }