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

<?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. dubbo系列六、SPI扩展Filter隐式传参

    一.实现Filter接口 1.消费者过滤器:ConsumerTraceFilter.java package com.dubbo.demo.Filter; import com.alibaba.dub ...

  2. sqlserver 日志传送

    可以使用日志传送将事务日志不间断地从一个数据库(主数据库)发送到另一个数据库(辅助数据库).不间断地备份主数据库中的事务日志,然后将它们复制并还原到辅助数据库,这将使辅助数据库与主数据库基本保持同步. ...

  3. 利用jstack命令定位占用cpu高的java线程及具体错误代码信息

    1.先用top查询某进程的线程CPU占用情况,定位到cpu占用高的进程pid 2.根据pid定位具体的线程top -p PID -H ,找出占用cpu最大的pid,此处占用cpu比较平均,我们随便选择 ...

  4. Expm 9_2 有向图的强连通分量问题

      [问题描述] 给定一个有向图,设计一个算法,求解并输出该图的各个强连通分量. package org.xiu68.exp.exp9; import java.util.ArrayList; imp ...

  5. js数值进制互转

    十进制转换为二进制: var num = 100; console.log(num.toString(2)); toString()方法可把一个 Number 对象转换为一个字符串,并返回结果. 语法 ...

  6. pytest十六:allure2 生成 html 报告

    allure 是一个 report 框架,支持 java 的 Junit/testng 等框架,当然也可以支持 python 的 pytest 框架,也可以集成到 Jenkins 上展示高大上的报告界 ...

  7. springMVC源码分析--页面跳转RedirectView(三)

    之前两篇博客springMVC源码分析--视图View(一)和springMVC源码分析--视图AbstractView和InternalResourceView(二)中我们已经简单的介绍了View相 ...

  8. BBC记录片之非洲4

  9. JDK和CGLIB生成动态代理类的区别

     关于动态代理和静态代理 当一个对象(客户端)不能或者不想直接引用另一个对象(目标对象),这时可以应用代理模式在这两者之间构建一个桥梁--代理对象. 按照代理对象的创建时期不同,可以分为两种: 静态代 ...

  10. 「BZOJ2882」工艺

    题解: 就是个最小表示法 大概做法就是扩大2倍原串 然后双指针比较,如果不相同了直接跳 原理随便画画就知道了