write() public static method

Writes a message to one or more log adapters, where the adapters that are written to are the ones that respond to the given priority level.
public static write ( string $priority, string $message, array $options = [] ) : boolean
$priority string The priority of the log message to be written.
$message string The message to be written.
$options array An array of adapter-specific options that may be passed when writing log messages. Some options are also handled by `Logger` itself: - `'name'` _string_: This option can be specified if you wish to write to a specific adapter configuration, instead of writing to the adapter(s) that respond to the given priority.
return boolean Returns `true` if all log writes succeeded, or `false` if _any or all_ writes failed.
示例#1
0
 /**
  * Tests the correct writing to the cache adapter. In this test we use the
  * "Memory" cache adapter so that we can easily verify the written message.
  */
 public function testWrite()
 {
     $message = "CacheLog test message...";
     $result = Logger::write('info', $message, array('name' => 'cachelog'));
     $this->assertNotEmpty($result);
     $result = CacheStorage::read('cachelog', 'cachelog_testkey');
     $this->assertEqual($message, $result);
 }
示例#2
0
 public static function __callStatic($priority = 'debug', $params = array())
 {
     $trace = Debugger::trace(array('format' => 'array', 'depth' => 3, 'includeScope' => false));
     $trace = $trace[2];
     if (empty($trace)) {
         throw UnexpectedValueException('Could not trace method');
     }
     $trace = array('method' => $trace['functionRef'], 'line' => $trace['line'], 'file' => $trace['file']);
     $message = "//////////// {$trace['file']}:{$trace['line']} -> {$trace['method']} ////////////\n";
     foreach ($params as $param) {
         $dump = Debugger::export($param);
         $message = "{$message}{$dump}\n";
     }
     return parent::write($priority, $message);
 }
 /**
  * Tests the writing mechanism. At first, no Response object is bound to the logger, so
  * it queues up the messages. When the Response object finally gets bound, it flushes the
  * needed headers and all messages at once. All messages coming after this point get added
  * to the header immediately.
  */
 public function testWrite()
 {
     $response = new Response();
     $result = Logger::write('debug', 'FirePhp to the rescue!', array('name' => 'firephp'));
     $this->assertTrue($result);
     $this->assertFalse($response->headers());
     $host = 'meta.firephp.org';
     $expected = array("X-Wf-Protocol-1: http://meta.wildfirehq.org/Protocol/JsonStream/0.2", "X-Wf-1-Plugin-1: http://{$host}/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.3", "X-Wf-1-Structure-1: http://{$host}/Wildfire/Structure/FirePHP/FirebugConsole/0.1", "X-Wf-1-1-1-1: 41|[{\"Type\":\"LOG\"},\"FirePhp to the rescue!\"]|");
     Logger::adapter('firephp')->bind($response);
     $this->assertEqual($expected, $response->headers());
     $result = Logger::write('debug', 'Add this immediately.', array('name' => 'firephp'));
     $this->assertTrue($result);
     $expected[] = 'X-Wf-1-1-1-2: 40|[{"Type":"LOG"},"Add this immediately."]|';
     $this->assertEqual($expected, $response->headers());
 }
 public function testIntegrationWriteFile()
 {
     $config = array('default' => array('adapter' => 'File'));
     Logger::config($config);
     $result = Logger::write('default', 'Message line 1');
     $this->assertTrue(file_exists(LITHIUM_APP_PATH . '/resources/tmp/logs/default.log'));
     $expected = "Message line 1\n";
     $result = file_get_contents(LITHIUM_APP_PATH . '/resources/tmp/logs/default.log');
     $this->assertEqual($expected, $result);
     $result = Logger::write('default', 'Message line 2');
     $this->assertTrue($result);
     $expected = "Message line 1\nMessage line 2\n";
     $result = file_get_contents(LITHIUM_APP_PATH . '/resources/tmp/logs/default.log');
     $this->assertEqual($expected, $result);
     unlink(LITHIUM_APP_PATH . '/resources/tmp/logs/default.log');
 }
 public function testWriteFilter()
 {
     $base = Libraries::get(true, 'resources') . '/tmp/logs';
     $this->skipIf(!is_writable($base), "Path `{$base}` is not writable.");
     Filters::apply('lithium\\analysis\\Logger', 'write', function ($self, $params, $chain) {
         $params['message'] = 'Filtered Message';
         return $chain->next($self, $params, $chain);
     });
     $config = array('default' => array('adapter' => 'File', 'timestamp' => false, 'format' => "{:message}\n"));
     Logger::config($config);
     $result = Logger::write('info', 'Original Message');
     $this->assertTrue(file_exists($base . '/info.log'));
     $expected = "Filtered Message\n";
     $result = file_get_contents($base . '/info.log');
     $this->assertEqual($expected, $result);
     unlink($base . '/info.log');
 }
示例#6
0
 public function testMultipleAdaptersWriteByNameSecondary()
 {
     $base = Libraries::get(true, 'resources') . '/tmp/logs';
     $this->skipIf(!is_writable($base), "Path `{$base}` is not writable.");
     Logger::config(array('default' => array('adapter' => 'File', 'file' => function ($data, $config) {
         return "{$data['priority']}_default.log";
     }, 'timestamp' => false, 'format' => "{:message}\n"), 'secondary' => array('adapter' => 'File', 'file' => function ($data, $config) {
         return "{$data['priority']}_secondary.log";
     }, 'timestamp' => false, 'format' => "{:message}\n")));
     $this->assertFalse(file_exists($base . '/info_secondary.log'));
     $this->assertTrue(Logger::write('info', 'Secondary Message line 1', array('name' => 'secondary')));
     $this->assertTrue(file_exists($base . '/info_secondary.log'));
     $expected = "Secondary Message line 1\n";
     $result = file_get_contents($base . '/info_secondary.log');
     $this->assertEqual($expected, $result);
     unlink($base . '/info_secondary.log');
 }
示例#7
0
 public function testWriteByName()
 {
     $base = LITHIUM_APP_PATH . '/resources/tmp/logs';
     $this->skipIf(!is_writable($base), "{$base} is not writable.");
     Logger::config(array('default' => array('adapter' => 'File', 'timestamp' => false, 'priority' => false)));
     $this->assertFalse(file_exists($base . '/info.log'));
     $this->assertFalse(Logger::write('info', 'Message line 1'));
     $this->assertFalse(file_exists($base . '/info.log'));
     $this->assertTrue(Logger::write(null, 'Message line 1', array('name' => 'default')));
     $expected = "Message line 1\n";
     $result = file_get_contents($base . '/.log');
     $this->assertEqual($expected, $result);
     unlink($base . '/.log');
 }
示例#8
0
 public function testWrite()
 {
     $result = Logger::write('info', 'SyslogTest message...', array('name' => 'syslog'));
     $this->assertNotEmpty($result);
 }
示例#9
0
<?php
use \lithium\data\Connections;
use \lithium\analysis\Logger;

Logger::config(array(
    'default' => array('adapter' => 'File')
));

/**
 * Log all queries passed to the MongoDB adapter.
 */
$MongoDb = Connections::get('default');
$MongoDb->applyFilter('read', function($self, $params, $chain) use (&$MongoDb) {
    $result = $chain->next($self, $params, $chain);

    if (method_exists($result, 'data')) {
        Logger::write('info',
            json_encode($params['query']->export($MongoDb) + array('result' => $result->data()))        );  
    }       return $result;
});
?>
示例#10
0
文件: ImageUtil.php 项目: qujian/rwe
 /**
  * @param $img_path 原图路径
  * @param $width 输出宽度
  * @param $height 输出高度
  * 
  * @return 输出图路径。如果转换失败则返回空字符串
  */
 public static function create_thumb_by_width_and_height($img_path, $width, $height, $quality = self::DEFAULT_IMG_THUMB_QUALITY)
 {
     $func_resize = '\\tencent\\best_image\\smart_resize';
     if (!function_exists($func_resize)) {
         \lithium\analysis\Logger::write('debug', ' create_thumb, function smart_resize is undefined. img_path: ' . $img_path . '   ' . PHP_EOL);
         return "";
     }
     $thumb_path = self::get_thumb_name($img_path, $width, $height);
     $func_resize($img_path, $thumb_path, $width, $height, $quality);
     if (is_file($thumb_path)) {
         chmod($thumb_path, FILE_MOD);
         return $thumb_path;
     } else {
         return "";
     }
 }
示例#11
0
 public function testWrite()
 {
     $result = Logger::write('syslog', 'SyslogTest message...');
     $this->assertTrue($result);
 }
示例#12
0
 /**
  * Logs a SQL statement somewhere.
  *
  * @param string $sql The SQL to be executed.
  * @param array $params The SQL parameters.
  * @param float $executionMS The microtime difference it took to execute this query.
  * @return void
  */
 public function logSQL($sql, array $params = null, $executionMS = null)
 {
     Logger::write('debug', $sql);
 }
示例#13
0
 /**
  * allows ajaxified upload of files
  *
  * @see
  * @filter
  * @param array $options [description]
  * @return [type] [description]
  */
 protected function _upload(array $options = array())
 {
     $defaults = array('allowed' => '*', 'path' => Libraries::get(true, 'resources') . '/tmp/cache', 'prefix' => __FUNCTION__, 'chmod' => 0644);
     $options += $defaults;
     if (!$this->request->is('ajax')) {
         return array('error' => 'only ajax upload allowed.');
     }
     if (empty($_GET['qqfile'])) {
         sscanf(str_replace('?', ' ', $this->request->env('REQUEST_URI')), '%s qqfile=%s', $t, $file);
     } else {
         $file = $_GET['qqfile'];
     }
     $pathinfo = pathinfo($file);
     $name = $pathinfo['filename'];
     $type = isset($pathinfo['extension']) ? $pathinfo['extension'] : '';
     if (!in_array($type, (array) $options['allowed']) && $options['allowed'] != '*') {
         $error = 'file-extension not allowed.';
         return compact('error', 'name', 'type');
     }
     $tmp_name = tempnam($options['path'], $options['prefix']);
     $input = fopen('php://input', 'r');
     $temp = fopen($tmp_name, 'w');
     $size = stream_copy_to_stream($input, $temp);
     @chmod($tmp_name, $options['chmod']);
     fclose($input);
     $msg = sprintf('upload of file %s.%s to %s', $name, $type, $tmp_name);
     $complete = (bool) ($size == (int) $_SERVER['CONTENT_LENGTH']);
     if (!$complete) {
         $msg = $error = $msg . ' failed.';
     } else {
         $msg = 'succesful ' . $msg;
         $error = UPLOAD_ERR_OK;
     }
     $data = compact('error', 'name', 'type', 'size', 'tmp_name');
     $priority = $complete ? 'debug' : 'warning';
     Logger::write($priority, $msg);
     return $data;
 }