写日志(log)
以下为我自己写的一个写日志的类,比较简洁。
<?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)的更多相关文章
- cocos2d-js 写日志log 查看日志log Android调试查看log
1 输出日志的方式,当然是cc.log了 2 如何查看日志? a)如果小程序可以先在浏览器上跑,例如用chrome,在控制台就可以看到输出的log: b)如果在真机上调试, ...
- 写日志 log 到文件夹
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- How To Write In Sharepoint Log File 怎么对自定义的MOSS代码写日志
How To Write In Sharepoint Log File 怎么对自定义的MOSS代码写日志 Add Microsoft.Office.Server dll in your project ...
- 【m从翻译os文章】写日志禁令Sqlnet.log和Listener.log
写日志禁令Sqlnet.log和Listener.log 参考原始: How to Disable Logging to the Sqlnet.log and the Listener.log (Do ...
- php 写内容到文件,把日志写到log文件
php 写内容到文件,把日志写到log文件 <?php header("Content-type: text/html; charset=utf-8"); /******** ...
- 快速php日志,写内容到文件,把日志写到log文件
php 写内容到文件,把日志写到log文件 //记录日志:要写入文件的文件名(可以是任意文件名),如果文件不存在,将会创建一个.log.txt位置在项目的根目录下. $file = 'log.txt' ...
- 如何正确使用日志Log
title: 如何正确使用日志Log date: 2015-01-08 12:54:46 categories: [Python] tags: [Python,log] --- 文章首发地址:http ...
- .NET Core的日志[5]:利用TraceSource写日志
从微软推出第一个版本的.NET Framework的时候,就在“System.Diagnostics”命名空间中提供了Debug和Trace两个类帮助我们完成针对调试和跟踪信息的日志记录.在.NET ...
- [转]ASP.NET Core 开发-Logging 使用NLog 写日志文件
本文转自:http://www.cnblogs.com/Leo_wl/p/5561812.html ASP.NET Core 开发-Logging 使用NLog 写日志文件. NLog 可以适用于 . ...
随机推荐
- linux 内核是什么?
一:linux系统如何构成的?User space:User Applications and GNU C library (glibc)kernel space:System Call interf ...
- PYTHON-面向对象 继承 派生
1. 什么是继承 继承是一种新建类的方式,新建的类称之为子类/派生类,被继承的类称之为父类/基类/超类 继承有3个特点: 1. 子类可以遗传/重用父类的属性(解决类与类之间代码冗余的问题) 2. 在p ...
- 转:vue+element实现树形组件
项目中需要用到树形组件,在网上发现一个用vue+element实现的树形组件,现在记录下: demo地址:https://github.com/wilsonIs/vue-treeSelect
- Android 截屏与 WebView 长图分享经验总结
最近在做新业务需求的同时,我们在 Android 上遇到了一些之前没有碰到过的问题,截屏分享. WebView 生成长图以及长图在各个分享渠道分享时图片模糊甚至分享失败等问题,在这过程中踩了很多坑,到 ...
- spark简单总结—短小精悍
Spark是基于内存计算的大数据并行计算框架.因为其基于内存计算,较Hadoop中MapReduce计算框架具有更高的实时性,同时保证了高效容错性和可伸缩性.从2009年诞生于AMPLab到现在已经成 ...
- LeetCode(50):Pow(x, n)
Medium! 题目描述: 实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10 输出: 1024.00000 示例 2: 输入: 2.10000, ...
- django----重定向
urlpatterns = [ re_path(r'^(\w+)(\w+)/$',views.index,name="index"), ] 1.<a href="{ ...
- 获取修改value
val() 方法,获取和修改有value属性的元素,有value属性的元素有input.botton.select等.相当于JavaScript中的value. <!DOCTYPE html&g ...
- SimInfo获取(MCC, MNC, PLMN)
String NUMERIC = getSIMInfo(); protected String getSIMInfo() { TelephonyManager iPhoneManager = (Tel ...
- C#之app.config、exe.config和vshost.exe.config作用区别
vshost.exe.config是程序运行时的配置文本 exe.config是程序运行后会复制到vshost.exe.config app.config是在vshost.exe.config和exe ...