ScribeLoggerAppender::shutdown()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
nop 0
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Agavi\Logging;
3
4
// +---------------------------------------------------------------------------+
5
// | This file is part of the Agavi package.                                   |
6
// | Copyright (c) 2005-2011 the Agavi Project.                                |
7
// |                                                                           |
8
// | For the full copyright and license information, please view the LICENSE   |
9
// | file that was distributed with this source code. You can also view the    |
10
// | LICENSE file online at http://www.agavi.org/LICENSE.txt                   |
11
// |   vi: set noexpandtab:                                                    |
12
// |   Local Variables:                                                        |
13
// |   indent-tabs-mode: t                                                     |
14
// |   End:                                                                    |
15
// +---------------------------------------------------------------------------+
16
use Agavi\Exception\LoggingException;
17
18
/**
19
 * ScribeLoggerAppender sends LoggerMessages to a Scribe server or an
20
 * interface using the Scribe Thrift protocol (Facebook Scribe, Cloudera Flume).
21
 *
22
 * Configuration parameters:
23
 *  'buffer'                 - Whether or not to buffer all messages and only
24
 *                             send them on shutdown. Defaults to false.
25
 *  'default_category'       - Default scribe category for messages ("default"),
26
 *                             can be overriden in a message using parameter
27
 *                             "scribe_category".
28
 *  'socket_host'            - Hostname of scribe server (default "localhost")
29
 *  'socket_port'            - Port of scribe server (default 1463)
30
 *  'socket_persist'         - Whether to use persistent conns (default false)
31
 *  'socket_timeout'         - Socket timeout in seconds (default The thrift default)
32
 *  'transport_strict_read'  - Strict protocol reads (default false)
33
 *  'transport_strict_write' - Strict protocol writes (default true)
34
 *
35
 * @package    agavi
36
 * @subpackage logging
37
 *
38
 * @author     David Zülke <[email protected]>
39
 * @copyright  Authors
40
 * @copyright  The Agavi Project
41
 *
42
 * @since      1.0.4
43
 *
44
 * @version    $Id$
45
 */
46
class ScribeLoggerAppender extends LoggerAppender
47
{
48
    /**
49
     * @var        scribeClient The scribeClient instance to write to.
50
     */
51
    protected $scribeClient = null;
52
    
53
    /**
54
     * @var        TTransport The Thrift transport instance to use.
55
     */
56
    protected $transport = null;
57
    
58
    /**
59
     * @var        array A buffer of messages to log
60
     */
61
    protected $buffer = array();
62
    
63
    /**
64
     * Retrieve the scribeClient instance to write to.
65
     *
66
     * @return     scribeClient The scribeClient instance to write to.
67
     *
68
     * @author     David Zülke <[email protected]>
69
     * @since      1.0.4
70
     */
71
    protected function getScribeClient()
72
    {
73
        if (!$this->scribeClient) {
74
            $socketClass = $this->getParameter('socket_class', 'TSocket');
75
            $socket = new $socketClass($this->getParameter('socket_host', 'localhost'), $this->getParameter('socket_port', 1463), $this->getParameter('socket_persist', false));
76
            if ($this->hasParameter('socket_timeout')) {
77
                // setRecvTimeout takes milliseconds
78
                $socket->setRecvTimeout(1000 * $this->getParameter('socket_timeout'));
79
            }
80
            
81
            $transportClass = $this->getParameter('transport_class', 'TFramedTransport');
82
            $this->transport = new $transportClass($socket);
83
            
84
            $protocolClass = $this->getParameter('protocol_class', 'TBinaryProtocol');
85
            $protocol = new $protocolClass($this->transport, $this->getParameter('transport_strict_read', false), $this->getParameter('transport_strict_write', true));
86
            
87
            $clientClass = $this->getParameter('client_class', 'scribeClient');
88
            $this->scribeClient = new $clientClass($protocol, $protocol);
89
            
90
            try {
91
                $this->transport->open();
92
            } catch (TException $e) {
0 ignored issues
show
Bug introduced by
The class Agavi\Logging\TException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
93
                $this->scribeClient = null;
94
                $this->transport = null;
95
                throw new LoggingException(sprintf("Failed to connect to Scribe server:\n\n%s", $e->getMessage()), 0, $e);
96
            }
97
        }
98
        
99
        return $this->scribeClient;
100
    }
101
102
    /**
103
     * Execute the shutdown procedure.
104
     *
105
     * Tells the Scribe client to flush and send a shutdown command.
106
     * Underlying sockets will be auto-closed at the end of the script.
107
     *
108
     * @author     David Zülke <[email protected]>
109
     * @since      1.0.4
110
     */
111
    public function shutdown()
112
    {
113
        try {
114
            $this->flush();
115
        } catch (LoggingException $e) {
116
            // not much we can do at this point...
117
        }
118
        if ($this->transport) {
119
            $this->transport->close();
120
        }
121
    }
122
123
    /**
124
     * Write log data to this appender.
125
     *
126
     * @param      LoggerMessage $message Log data to be written.
127
     *
128
     * @throws     LoggingException if no Layout is set or the stream
129
     *                                          cannot be written.
130
     *
131
     *
132
     * @author     David Zülke <[email protected]>
133
     * @since      1.0.4
134
     */
135
    public function write(LoggerMessage $message)
136
    {
137
        if (($layout = $this->getLayout()) === null) {
138
            throw new LoggingException('No Layout set');
139
        }
140
        
141
        $this->buffer[] = new LogEntry(array(
142
            'category' => $message->getParameter('scribe.category', $this->getParameter('default_category', 'default')),
143
            'message' => (string)$this->getLayout()->format($message),
144
        ));
145
        
146
        if (!$this->getParameter('buffer', false)) {
147
            $this->flush();
148
        }
149
    }
150
    
151
    /**
152
     * Send buffer contents if there are any.
153
     *
154
     * @author     David Zülke <[email protected]>
155
     * @since      1.0.4
156
     */
157
    protected function flush()
158
    {
159
        if (!$this->buffer) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->buffer of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
160
            // nothing to send
161
            return;
162
        }
163
        
164
        $this->getScribeClient()->Log($this->buffer);
165
        
166
        $this->buffer = array();
167
    }
168
}
169