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

<?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. OneNET麒麟座应用开发之八:采集大气压力等环境参数

    采集大气压力和温度也是核算大气标准状况下的各种数据的必须参数,为此我们必须知道压力和温度才能计算标准状况下的各种参数,于此我们需要一个既能检测压力也能检测温度的元件. 1.硬件概述 MS5837压力传 ...

  2. tcpdump使用示例

    前言 这段时间一直在研究kubernetes当中的网络, 包括通过keepalived来实现VIP的高可用时常常不得不排查一些网络方面的问题, 在这里顺道梳理一下tcpdump的使用姿势, 若有写的不 ...

  3. gcd,lcm

    定理:gcd(a,b)*lcm(a,b)=a*b; 更相损减术:gcd(a,b)=gcd(b,a-b)=gcd(a,a-b) 欧几里得算法:gcd(a,b)=gcd(b,a mod b) 复杂度O(l ...

  4. js中数组去重

    编写函数norepeat(arr) 将数组的重复元素去掉,并返回新的数组 [注]正序去重,会漏掉一些元素. [注]去重倒序. var arr = [10, 20, 30, 40, 30, 20, 20 ...

  5. 字节、字、bit、byte的关系【转】

    字 word 字节 byte 位 bit 字长是指字的长度 1字=2字节(1 word = 2 byte) 1字节=8位(1 byte = 8bit)  一个字的字长为16 一个字节的字长是8 bps ...

  6. java List.subList方法中的超级大陷阱

    ArrayList 中 subList 的基本用法: subList(fromIndex:int,toIndex:int):List<E> 返回从fromIndex到toindex-1 的 ...

  7. ERP合同审核流程处理(二十九)

    合同审批流程: 前端的代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind=" ...

  8. hdu 1548 升降梯

    题目大意:有一个升降机,它有两个按钮UP和DOWN,给你一些数i表示层数,并且每层对应的Ki,如果按UP按钮,会从第i层升到第i+Ki层:如果按了DOWN则会从第i层降到第i-Ki层:并规定能到的层数 ...

  9. hdu 1025 上面n个点与下面n个点对应连线 求最多能连有多少条不相交的线 (LIS)

    题目大意有2n个城市,其中有n个富有的城市,n个贫穷的城市,其中富有的城市只在一种资源富有,且富有的城市之间富有的资源都不相同,贫穷的城市只有一种资源贫穷,且各不相同,现在给出一部分贫穷城市的需求,每 ...

  10. Ubuntu 安装 OpenMPI

    1. 下载OpenMPI 在官网上下载最新版本的安装包,如:openmpi-1.8.4.tar.gz 2. 解压并进行配置 tar -zxvf openmpi-3.0.0.tar.gz cd open ...