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

<?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. 读SRE Google运维解密有感(一)

    前言 这几天打算利用碎片时间读了一下"SRE Google运维解密"这本书,目前读了前几章,感觉收获颇多,结合自己的工作经历和书中的要点,写一些感悟和思考 SRE 有关SRE我就不 ...

  2. 2018-2019-2-20175225 实验二《Java开发环境的熟悉》实验报告

    姓名:张元瑞 学号:20175225 班级:1752 实验课程:JAVA程序设计 实验名称:Java面向对象程序设计 实验时间:2019.4.16 指导老师:娄嘉鹏 实验内容 测试点一 - " ...

  3. HTML中的锚点设置和table格式

    锚点设置: <a href="#1">锚点</a> <a name="1"></a> table表格格式: &l ...

  4. Webpack devServer中的 proxy 实现跨域

    Webpack dev server使用http-proxy解决跨域问题 文档资料 webpack关于webpack-dev-server开启proxy的官方介绍Vue-cli proxyTable ...

  5. PHP魔术方法实例

    PHP中把以两个下划线__开头的方法称为魔术方法,这些方法在PHP中充当了举足轻重的作用. 魔术方法包括: __construct(),类的构造函数 __destruct(),类的析构函数 __cal ...

  6. codeforce 139E

    成段更新+离散化才能过,数据好强.. 单点更新挂在了test27,下次做到成段更新再来做! /* 期望=存活概率*点权值/100 ans=sum(期望) 离散化树木权值,数轴统计累加可能倒下的树木概率 ...

  7. bzoj 2142

    数论大集合 只要你做完了这道题,除了线性筛和降幂公式以外,所有数论noip知识点就都会了... 题意:求C(n,∑w)*C(∑w,w1)*C(∑w-w1,w2).....mod p(不保证p为质数) ...

  8. 统计uv(转)

    UV是unique visitor的简写,是指通过互联网访问.浏览这个网页的自然人.在同一天内,uv只记录第一次进入网站的具有独立IP的访问者,在同一天内再次访问该网站则不计数.独立IP访问者提供了一 ...

  9. 如何学习 JavaScript?

    转自:https://www.zhihu.com/question/21064817 首先要说明的是,咱现在不是高手,最多还是一个半桶水,算是入了JS的门. 谈不上经验,都是一些教训. 这个时候有人要 ...

  10. poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题

    poj 1251  && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...