以下为我自己写的一个写日志的类,比较简洁。

<?php
class Log
{
/**
* @Purpose : 写日志
* @Method Name : writeLog()
* @parameter : string $explain 错误说明
* string $error_location 错误位置
* string $dir 保存日志的文件夹名
* @return : (无)
*/
function writeLog($explain,$error_location,$dir){
if (empty($dir)) return false;
$error_msg = "";
if(strlen($explain) != 0){
$error_msg .= '['.date("Y-m-d H:i:s",time()).' error_explain: ] '. $explain;
}
if(strlen($error_location) != 0){
$error_msg .= ' [ error_location: ] '. $error_location;
}
if(! is_dir($dir)){
mkdir($dir,0777);
system("chown root.root {$dir} -R;chmod 777 {$dir} -R"); // 修改目录所有者,所属组和权限
}
$file_name = date('Y-m-d',time()).'.txt';
$file_name = $dir.$file_name;
if(! $file_name == false){
system("chown root.root {$file_name} -R; chmod 777 {$file_name} -R");
$handle1 = fopen($file_name,'w');
fwrite($handle1,"//log file , please do not modify me! \n");
fwrite($handle1,'//Created on '.date('M j, Y, G:i',time())."\n"); // 类似于这种格式 :Created on Jun 26,2017, 11:22
fclose($handle1);
}
if(is_file($file_name)){
$handle2 = fopen($file_name,'a');
fwrite($handle2,"$error_msg \n");
fclose($handle2);
}
/*
最后打印出来的日志类似于这样:
//log file , please do not modify me!
//Created on Jun 26, 2017, 11:34
[2017-06-26 11:34:29 error_explain: ] test_explain [ error_location: ] the error at E:\www\test\index.php line 55
*/
} /**
* @Purpose : 获取错误所在位置
* @Method Name : getErrorLine()
* @Parameters : string $line 行号
* @Return array : $error_line 错误所在位置
*/
public function getErrorLine($line)
{
$error_line = __FILE__ . ' line ' . $line ;
return ' '.$error_line;
}
}
$Log = new Log();
$Log -> writeLog('test_explain','the error at' . $Log -> getErrorLine(__LINE__),'E:/www/test/log/');

下面这个是一个单独的方法:

    /**
* @purpose : 写日志
* @param : string $data : 写入的数据
* : string $logDir : 日志目录
* : string $file : 文件名前缀
* @Author : daicr
* @Time : 2018-11-20
*/
public function writeLog($data,$logDir='/tmp/', $fileName='') {
$write_line = "";
$now = date('Y-m-d H:i:s',time());
if(strlen($data)>0) {
$write_line .= "[" . date('Y-m-d H:i:s',time()) . "]" . $data;
}
$dir = $logDir;
if(!is_dir($dir)) {
mkdir($dir, 0777);
}
system("chown justswitch.justswitch {$dir} -R; chmod 777 {$dir} -R;"); $fileName = $fileName.date('Y-m',time());
$fileName = $dir.$fileName.".txt";
if (false==file_exists($fileName)){
if($fp = fopen("$fileName", 'w')) {
system("chown justswitch.justswitch -R $dir;chmod 777 $dir -R;");
fwrite($fp, "\n//JUST-CALL! log file, DO NOT modify me!\n".
"//Created on ".date("M j, Y, G:i")."\n");
fclose($fp);
}
}
if($fp = fopen("$fileName",'a')) {
fwrite($fp,"$write_line\n");
fclose($fp);
} system("chown justswitch.justswitch {$fileName};chmod 777 {$fileName};");
}

system($commond, $return_var)

commond   : 要执行的命令

return_var  : 外部命令执行后要返回的状态

成功则返回命令输出的最后一行,失败则返回 false

本文为原创作品,如有转载请注明出处http://www.cnblogs.com/chrdai/p/7082146.html

写日志(log)的更多相关文章

  1. cocos2d-js 写日志log 查看日志log Android调试查看log

    1 输出日志的方式,当然是cc.log了 2 如何查看日志?        a)如果小程序可以先在浏览器上跑,例如用chrome,在控制台就可以看到输出的log:        b)如果在真机上调试, ...

  2. 写日志 log 到文件夹

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  3. How To Write In Sharepoint Log File 怎么对自定义的MOSS代码写日志

    How To Write In Sharepoint Log File 怎么对自定义的MOSS代码写日志 Add Microsoft.Office.Server dll in your project ...

  4. 【m从翻译os文章】写日志禁令Sqlnet.log和Listener.log

    写日志禁令Sqlnet.log和Listener.log 参考原始: How to Disable Logging to the Sqlnet.log and the Listener.log (Do ...

  5. php 写内容到文件,把日志写到log文件

    php 写内容到文件,把日志写到log文件 <?php header("Content-type: text/html; charset=utf-8"); /******** ...

  6. 快速php日志,写内容到文件,把日志写到log文件

    php 写内容到文件,把日志写到log文件 //记录日志:要写入文件的文件名(可以是任意文件名),如果文件不存在,将会创建一个.log.txt位置在项目的根目录下. $file = 'log.txt' ...

  7. 如何正确使用日志Log

    title: 如何正确使用日志Log date: 2015-01-08 12:54:46 categories: [Python] tags: [Python,log] --- 文章首发地址:http ...

  8. .NET Core的日志[5]:利用TraceSource写日志

    从微软推出第一个版本的.NET Framework的时候,就在“System.Diagnostics”命名空间中提供了Debug和Trace两个类帮助我们完成针对调试和跟踪信息的日志记录.在.NET ...

  9. [转]ASP.NET Core 开发-Logging 使用NLog 写日志文件

    本文转自:http://www.cnblogs.com/Leo_wl/p/5561812.html ASP.NET Core 开发-Logging 使用NLog 写日志文件. NLog 可以适用于 . ...

随机推荐

  1. Redis简介+常用命令

    Redis=REmote DIctionary Server Redis是一个使用C语言编写的开源数据库,是高性能的key-value数据库,是内存数据库,支持数据持久化. Redis常用数据类型: ...

  2. 【bzoj1901】dynamic ranking(带修改主席树/树套树)

    题面地址(权限题) 不用权限题的地址 首先说说怎么搞带修改主席树? 回忆一般的kth问题,我们的主席树求的是前缀和,这样我们在目标区间的左右端点的主席树差分下就能求出kth. 那么我们如何支持修改操作 ...

  3. vue系列之webstrom的设置

    1.安装vue插件,方法 Setting->Plugins,点击Plugins,在右边输入vue,找到相应插件,然后安装 2.创建vue模板 注意红圈里面的 3.设置vue文件支持的样式 注意: ...

  4. 在vscode成功配置Python环境

    注意:如果您希望在Visual Studio Code中开始使用Python,请参阅教程.本文仅关注设置Python解释器/环境的各个方面. Python中的“环境”是Python程序运行的上下文.环 ...

  5. Java列表、数组、字符串

    列表(list) list中添加,获取,删除元素 添加方法是:.add(e): 获取方法是:.get(index): 删除方法是:.remove(index), 按照索引删除: .remove(Obj ...

  6. python 全栈开发,Day117(popup,Model类的继承,crm业务开发)

    昨日内容回顾 第一部分:权限相关 1. 权限基本流程 用户登录成功后获取权限信息,将[权限和菜单]信息写入到session. 以后用户在来访问,在中间件中进行权限校验. 为了提升用户体验友好度,在后台 ...

  7. python 全栈开发,Day114(装饰器,排序规则,显示列,添加按钮,定制ModelForm,自定义列表页面,自定制URL)

    一.装饰器 装饰器本质上就是一个python函数,他可以让其他函数在不需要做任何代码变动的前提下,增加额外的功能,装饰器的返回值也是一个函数对象. 装饰器的应用场景:比如插入日志,性能测试,事务处理, ...

  8. php手动搭建wamp环境(一)--之 Apache HTTP Servcer-Apache

    前言:因为之前为了快速进入工作(其实是懒!),所以一直使用集成环境 wampserver  或 phpstudy,傻瓜式安装使用卸载,配置也简单.但是最近新电脑突然想回顾一下手动安装,所以记录一下.搭 ...

  9. mySQL 增量备份方案

    1.在 /etc/my.cnf 下面设置开启bin-log 编辑 vim /etc/my.cnf [mysqld] binlog_format       = MIXED                ...

  10. BZOJ1179 [Apio2009]Atm Tarjan 强连通缩点 动态规划

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1179 题意概括 有一个有向图,每一个节点有一个权值,其中有一些结束点. 现在,你要从S出发,到达任 ...