1: <?php
2: /**
3: * Fluent-PHP-Logger
4: *
5: * Copyright (C) 2011 Shuhei Tanuma
6: *
7: * Licensed under the Apache License, Version 2.0 (the "License");
8: * you may not use this file except in compliance with the License.
9: * You may obtain a copy of the License at
10: *
11: * http://www.apache.org/licenses/LICENSE-2.0
12: *
13: * Unless required by applicable law or agreed to in writing, software
14: * distributed under the License is distributed on an "AS IS" BASIS,
15: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16: * See the License for the specific language governing permissions and
17: * limitations under the License.
18: */
19: namespace Fluent\Logger;
20:
21: /**
22: * Fluent HTTP logger class.
23: *
24: * @author Shuhei Tanuma <stanuma@zynga.com>
25: */
26: class HttpLogger extends BaseLogger
27: {
28: const DEFAULT_HTTP_PORT = 8888;
29:
30: protected $tag;
31: protected $host;
32: protected $port;
33:
34: /**
35: * create fluent http logger object.
36: *
37: * @param string $tag primary tag
38: * @param string $host
39: * @param int $port
40: * @return HttpLogger
41: */
42: public function __construct($tag, $host, $port = HttpLogger::DEFAULT_HTTP_PORT)
43: {
44: $this->tag = $tag;
45: $this->host = $host;
46: $this->port = $port;
47: }
48:
49: /**
50: * Fluent singleton API.
51: *
52: * @todo fixed singleton api.
53: * @param string $tag primary tag
54: * @param string $host
55: * @param int $port
56: * @return HttpLogger created http logger object.
57: */
58: public static function open($tag, $host, $port = HttpLogger::DEFAULT_HTTP_PORT)
59: {
60: $logger = new self($tag,$host,$port);
61: return $logger;
62: }
63:
64: /**
65: * send a message to specified fluentd.
66: *
67: * @param mixied $data
68: */
69: public function post($data, $additional = null)
70: {
71: $packed = json_encode($data);
72: $request = sprintf('http://%s:%d/%s?json=%s', $this->host, $this->port, $this->tag, urlencode($packed));
73:
74: file_get_contents($request);
75: }
76: }
77: