【问题】
折腾:
【已解决】将Log函数的参数改变为类似于sprintf的动态个数的可变参数
期间,已经调试了半天了,
对于如下代码:
(1)crifanLib.php
<?php /* [Filename] crifanLib.php [Function] crifan's php lib, implement common functions [Author] Crifan Li [Contact]联系我[Note] 1.online see code: http://code.google.com/p/crifanlib/source/browse/trunk/php/crifanLib.php [TODO] [History] [v2015-07-27] 1.add logInit, logWrite [v1.0] 1.initial version, need clean up later */ class crifanLib { private $logFile; function __construct() { $this->logInit(); } function __destruct() { } /*********************************************************************************************** * String/Path ***********************************************************************************************/ /* * 【已解决】PHP中如何实现路径拼接(两个路径合并)以及合并文件夹路径和文件名 * https://www.crifan.org/php_path_concatenation_combine_directory_and_filename * eg: * from: * D:\tmp\WordPress\DevRoot\httpd-2.2.19-win64\httpd-2.2-x64\htdocs\php_test\35934503_data * cookie_jar.txt * to: * D:\tmp\WordPress\DevRoot\httpd-2.2.19-win64\httpd-2.2-x64\htdocs\php_test\35934503_data\cookie_jar.txt */ function concatenatePath($headPath, $tailPath) { $realHeadPath = realpath($headPath); // printAutoNewline("realHeadPath=".$realHeadPath); //$realTailPath = realpath($tailPath); //printAutoNewline("realTailPath=".$realTailPath); //$concatnatedPath = $realHeadPath.DIRECTORY_SEPARATOR.$realTailPath; // printAutoNewline("tailPath=".$tailPath); $concatnatedPath = $realHeadPath.DIRECTORY_SEPARATOR.$tailPath; // printAutoNewline("concatnatedPath=".$concatnatedPath); return $concatnatedPath; } /*********************************************************************************************** * Log ***********************************************************************************************/ /* Init log file */ function logInit($inputLogFile = null) { // set default log file name, path is current php file //logFolder=/var/www/1.2.3.4/public_html/php/access_token $logFolder = getcwd(); //logFilename=log_20150727131103.log $logFilename = "log_".date('YmdHis').".log"; //defautLogFile=/var/www/1.2.3.4/public_html/php/access_token/log_20150727131103.log $defautLogFile = $this->concatenatePath($logFolder, $logFilename); $this->logFile = $inputLogFile ? $inputLogFile : $defautLogFile; } /* Write log info to file */ function logWrite($logFormat, $logArgs = ()) { echo "Into logWrite"; print("logFormat=".$logFormat.'\r\n'); print("logArgs=".$logArgs.'\r\n'); $logFormatedContent = vsprintf($logFormat, $logArgs); print("logFormatedContent=".$logFormatedContent.'\r\n'); // define script name $scriptName = pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME); // define current time and suppress E_WARNING if using the system TZ settings // (don't forget to set the INI setting date.timezone) $timeStr = @date('[Y-m-d H:i:s]'); // write current time, script name and message to the log file //[2015-07-27 13:11:03] (wx_access_token) This is crifanLib log test file_put_contents($this->logFile, "$timeStr ($scriptName) $logFormatedContent" . PHP_EOL, FILE_APPEND); } /** * add newline for print */ function printAutoNewline($contentToPrint) { print_r($contentToPrint."<br />"); } } ?>
wx_access_token.php
<?php
/*
File: wx_access_token.php
Author: Crifan Li
Version: 2015-07-27
Contact: https://www.crifan.org/about/me/
Function: Wechat get access token
*/
include_once "crifanLib.php";
echo "before define";
//global definition
// define("TOKEN", "didaosuzhou");
define("APPID", "xxxxx"); //18 characters
// define("EncodingAESKey", "xxx"); //43 characters
define("APPSECRET", "xxxxx"); //32 characters
echo "end of define";
// https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
$crifanLib = new crifanLib();
echo "after new crifanlb";
$crifanLib->logWrite("This is crifanLib log test message not pass log file name");
$tokenUrlPattern = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
echo $tokenUrlPattern;
$getTokenUrl = sprintf($tokenUrlPattern, APPID, APPSECRET);
print($getTokenUrl);
$crifanLib->logWrite("getTokenUrl=%s", $getTokenUrl);
?>去运行wx_access_token.php,但是没有任何输出:
【折腾过程】
1.看来只是单独添加调试代码,echo,print,则是效果不好。
2.想办法,去看服务器上面的PHP是否正常运行,web服务器是否正常,以及PHP的相关log。
root@chantyou:access_token# service httpd status
Redirecting to /bin/systemctl status httpd.service
httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled)
Active: active (running) since Sat 2015-07-25 15:46:17 CST; 2 days ago
Process: 2224 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
Main PID: 1418 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─1418 /usr/sbin/httpd -DFOREGROUND
├─2232 /usr/sbin/httpd -DFOREGROUND
├─2233 /usr/sbin/httpd -DFOREGROUND
├─2234 /usr/sbin/httpd -DFOREGROUND
├─2235 /usr/sbin/httpd -DFOREGROUND
├─2236 /usr/sbin/httpd -DFOREGROUND
├─2587 /usr/sbin/httpd -DFOREGROUND
├─3749 /usr/sbin/httpd -DFOREGROUND
└─4655 /usr/sbin/httpd -DFOREGROUND
Jul 25 15:46:17 chantyou.com httpd[1418]: AH00548: NameVirtualHost has no effect and will be removed in the next release /e...conf:1
Jul 25 15:46:17 chantyou.com httpd[1418]: AH00558: httpd: Could not reliably determine the server's fully qualified domain ...essage
Jul 25 15:46:17 chantyou.com systemd[1]: Started The Apache HTTP Server.
Jul 26 03:48:01 chantyou.com systemd[1]: Reloading The Apache HTTP Server.
Jul 26 03:48:01 chantyou.com httpd[2224]: AH00548: NameVirtualHost has no effect and will be removed in the next release /e...conf:1
Jul 26 03:48:01 chantyou.com httpd[2224]: AH00558: httpd: Could not reliably determine the server's fully qualified domain ...essage
Jul 26 03:48:01 chantyou.com systemd[1]: Reloaded The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
root@chantyou:access_token# php -m
[PHP Modules]
bz2
calendar
Core
ctype
curl
date
dom
ereg
exif
fileinfo
filter
ftp
gd
gettext
gmp
hash
iconv
json
libxml
mcrypt
mhash
mysql
mysqli
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
readline
Reflection
session
shmop
SimpleXML
sockets
SPL
sqlite3
standard
tokenizer
wddx
xml
xmlreader
xmlwriter
xsl
zip
zlib
[Zend Modules]看起来是:
web服务器是正常的
PHP也是正常的。
3.再去看PHP的log:
【基本解决】CentOS 7中查看PHP运行时的Log文件日志信息
4.然后继续去试试运行PHP代码,看看是否log中有错误日志输出。
结果还是没有任何log输出。
5.然后直接去用:
echo "In" . __FILE__;
是可以输出结果的:
In/var/www/....../public_html/php/access_token/wx_access_token.php
6.再用:
echo "In" . __FILE__; include_once "crifanLib.php"; echo "include OK";
结果就出错,后面的echo不显示了。
把:
function logWrite($logFormat, $logArgs = ()) {变成:
function logWrite($logFormat) {试试:
结果真的就
include OK
了。
7.所以,此处还是要去研究那个:
然后就解决了此处的问题了。
【总结】
此处是由于,PHP中初始化数组为空,写成了
$logArgs = ()
导致语法出错,然后结果PHP代码运行没有任何输出。
改为
$logArgs = []
或:
$logArgs = array()
都可以。
转载请注明:在路上 » 【已解决】PHP代码尝试使用vsprintf期间出错无任何输出