1: <?php
2: /**
3: * Fluent-Logger-PHP
4: *
5: * Copyright (C) 2011 - 2012 Fluent-Logger-PHP Contributors
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: class HttpLogger extends BaseLogger
25: {
26: const DEFAULT_HTTP_PORT = 8888;
27:
28: protected $tag;
29: protected $host;
30: protected $port;
31:
32: /**
33: * create fluent http logger object.
34: *
35: * @param string $host
36: * @param int $port
37: * @return HttpLogger
38: */
39: public function __construct($host, $port = HttpLogger::DEFAULT_HTTP_PORT)
40: {
41: $this->host = $host;
42: $this->port = $port;
43: }
44:
45: /**
46: * fluent-logger compatible API.
47: *
48: * @param string $host
49: * @param int $port
50: * @return HttpLogger created http logger object.
51: */
52: public static function open($host, $port = HttpLogger::DEFAULT_HTTP_PORT)
53: {
54: $logger = new self($host,$port);
55: return $logger;
56: }
57:
58: /**
59: * send a message to specified fluentd.
60: *
61: * @todo use HTTP1.1 protocol and persistent socket.
62: * @param string $tag
63: * @param array $data
64: */
65: public function post($tag, array $data)
66: {
67: $packed = json_encode($data);
68: $request = sprintf('http://%s:%d/%s?json=%s', $this->host, $this->port, $tag, urlencode($packed));
69:
70: file_get_contents($request);
71: }
72:
73: /**
74: * send a message to specified fluentd.
75: *
76: * @todo use HTTP1.1 protocol and persistent socket.
77: * @param string $tag
78: * @param array $data
79: */
80: public function post2(Entity $entity)
81: {
82: $packed = json_encode($entity->getData());
83: $request = sprintf('http://%s:%d/%s?json=%s', $this->host, $this->port, $entity->getTag(), urlencode($packed));
84:
85: file_get_contents($request);
86: }
87:
88: }
89: