Skip to content

Commit

Permalink
设置默认文件保存为runtime,开发环境和生产环境使用不同权限设置
Browse files Browse the repository at this point in the history
  • Loading branch information
NewFuture committed Aug 16, 2016
1 parent b986106 commit a38752b
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 70 deletions.
13 changes: 12 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
#tempFile
#临时文件
temp/
runtime/

#vagrant
.vagrant/
Vagrantfile
start.cmd
stop.cmd

# 启动脚本
# script
server.cmd

#editor
.vscode/
.atom/
sftp-config.json


#localfile
#本地文件
#secret config
*.local
conf/secret.product.ini

# Windows image file caches
Thumbs.db
Expand Down Expand Up @@ -56,4 +68,3 @@ $RECYCLE.BIN/
Network Trash Folder
Temporary Items
.apdisk
conf/secret.product.ini
2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Yunyin Yaf Framework
│ index.php 入口文件
│ robots.txt
└─temp 缓存日志临时文件夹【可写权限
└─runtime 文件缓存日志临时文件夹【保证程序具有可读写权限
```
>
Expand Down
20 changes: 18 additions & 2 deletions app/Bootstrap.dev.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
use \Storage\File as File;

/**
* 调试启动加载
Expand Down Expand Up @@ -64,7 +65,7 @@ public function _initDebug()

case 'log': //log到文件
ini_set('log_errors', 1);
ini_set('error_log', Config::get('tempdir') . '/error_log.txt');
ini_set('error_log', Config::get('runtime') . '/error_log.txt');
break;

default:
Expand Down Expand Up @@ -107,7 +108,7 @@ public function _initSqlLog()
}

/**
* 开启调试输出
* 添加路由
* @method _initRoute
* @author NewFuture
*/
Expand All @@ -118,6 +119,21 @@ public function _initRoute(Yaf_Dispatcher $dispatcher)
}
}


/**
* 文件权限
* @method _initMode
* @author NewFuture
*/
public function _initMode()
{
if ($mode = Config::get('log.mode')) {
$mode = intval($mode, 8);
Logger::$mode = $mode;
File::$mode = $mode;
}
}

/**
* 加载插件
* @method _initPlugin
Expand Down
36 changes: 18 additions & 18 deletions app/controllers/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
*/
class ErrorController extends Yaf_Controller_Abstract
{
public function errorAction($exception)
{
Yaf_Dispatcher::getInstance()->disableView();
$code = $exception->getCode()?:500;
$url = $this->_request->getRequestUri();
Logger::error('[exception: {code}]({url}): {exception}',array(
'code'=>$code,
'url'=>$url,
'exception'=>$exception->__toString(),
));
$response['status'] = -10;
$response['info'] = ['code' => $code, 'msg' => '请求异常!', 'uri' => $url];
public function errorAction($exception)
{
Yaf_Dispatcher::getInstance()->disableView();
$code = $exception->getCode()?:500;
$url = $this->_request->getRequestUri();
Logger::error('[exception: {code}]({url}): {exception}', array(
'code' => $code,
'url' => $url,
'exception'=> $exception->__toString(),
));
$response['status'] = -10;
$response['info'] = ['code' => $code, 'msg' => '请求异常!', 'uri' => $url];

header('Content-Type: application/json; charset=utf-8',true,$code);
echo json_encode($response, JSON_UNESCAPED_UNICODE); //unicode不转码
}
}
header('Content-Type: application/json; charset=utf-8', true, $code);
echo json_encode($response, JSON_UNESCAPED_UNICODE); //unicode不转码
}
}
5 changes: 3 additions & 2 deletions conf/app.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[common];公用配置
version = 2.4.1
version = 2.4.2
application.directory = APP_PATH "/app/"
application.library = APP_PATH "/library"
application.num_param = 'id' ;id形默认绑定参数 如 /User/123 =>绑定参数$id值未123
Expand All @@ -9,7 +9,7 @@ application.dispatcher.defaultController = 'Index';默认控制器名称

;配置路径
secret_config_path = APP_PATH "/conf/secret.common.ini"
tempdir = APP_PATH "/temp/";临时文件目录和日志,缓存存于此处
runtime = APP_PATH "/runtime/";运行时文件目录和日志,缓存存于此处,生产环境可以设置别处

;上传通用配置
upload.type = 'qiniu'
Expand Down Expand Up @@ -74,6 +74,7 @@ kv.type = 'file'
;日志设置,支持系统日志(system),
log.type = 'file';日志类型
log.allow = 'EMERGENCY,ALERT,CRITICAL,ERROR,WARN,NOTICE,INFO,DEBUG,SQL,TRACER';允许记录的级别可以自定义
log.mode = '0777';文件权限,仅对开发环境设置有效

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
[product:common];线上(生产环境)配置
Expand Down
26 changes: 11 additions & 15 deletions init.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,12 @@ GOTO :EOF
:CLEAN_TEMP
IF NOT EXIST temp MKDIR temp
IF EXIST temp\cache (RMDIR /s/q temp\cache)
IF EXIST temp\kv (RMDIR /s/q temp\kv)
IF EXIST temp\log (RMDIR /s/q temp\log)
IF NOT EXIST runtime MKDIR runtime
IF EXIST runtime\cache (RMDIR /s/q runtime\cache)
IF EXIST runtime\kv (RMDIR /s/q runtime\kv)
IF EXIST runtime\log (RMDIR /s/q runtime\log)
MKDIR temp\cache temp\kv temp\log
ECHO Temp folders have been cleaned up.
ECHO runtime folders have been cleaned up.
GOTO :EOF
Expand Down Expand Up @@ -314,19 +312,17 @@ fi;


CLEAN_TEMP(){
if [ ! -d "temp" ]; then
mkdir temp;
if [ ! -d "runtime" ]; then
mkdir runtime;
fi;
folders=("cache" "kv" "log");
for f in "${folders[@]}"; do
if [ -d "temp/"$f ]; then
rm -r "temp/"$f;
else
mkdir "temp/"$f;
if [ -d "runtime/"$f ]; then
rm -rf "runtime/"$f;
fi
done;
chmod -R 755 temp;
echo "Temp folders have been cleaned up !" ;
chmod 777 runtime;
echo "runtime folders have been cleaned up !" ;
}


Expand Down
2 changes: 1 addition & 1 deletion library/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected static function Handler()
break;

case 'file': //文件缓存
self::$_handler = new Storage\File(Config::get('tempdir') . 'cache', true);
self::$_handler = new Storage\File(Config::get('runtime') . 'cache', true);
break;

default:
Expand Down
1 change: 1 addition & 0 deletions library/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public static function connect($config='_')
} catch (Exception $e) {
Logger::log('ALERT', '[Db::connect]数据库[{KEY}]({DSN})无法连接:{MSG}',
array('KEY'=>$key, 'DSN'=>$config['dsn'], 'MSG'=>$e->getMessage()));
throw $e;
}
}

Expand Down
2 changes: 1 addition & 1 deletion library/Kv.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected static function Handler()
self::$_handler = memcache_init();
break;
case 'file': //文件缓存
self::$_handler = new Storage\File(Config::get('tempdir') . 'kv', false);
self::$_handler = new Storage\File(Config::get('runtime') . 'kv', false);
break;

default:
Expand Down
51 changes: 31 additions & 20 deletions library/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ class Logger
* @var callable
*/
public static $listener = null;
/**
* 日志和日志目录权限,默认只允许php读写
* @var callable
*/
public static $mode = 0700;

private static $_conf = null;
private static $_stream = null;
private static $_dir = null;
private static $_files = null;

/**
* 写入日志
Expand Down Expand Up @@ -54,11 +58,12 @@ public static function write($msg, $level = 'NOTICE')
if (in_array($level, $config['allow'])) {
switch ($config['type']) {
case 'system':// 系统日志
return error_log($level.':'.$msg);
return error_log('['.$level .']:'. $msg);
case 'sae'://sae日志
return sae_debug($level .':'. $msg);
return sae_debug('['.$level .']:'. $msg);
case 'file'://文件日志
return fwrite(Logger::getStream($level), '[' . date('c') . '] ' . $msg . PHP_EOL);
$msg='[' . date('c') . '] ' . $msg . PHP_EOL;
return file_put_contents(Logger::getFile($level), $msg, FILE_APPEND);
default:
throw new Exception('未知日志类型' . $config['type']);
}
Expand All @@ -67,31 +72,37 @@ public static function write($msg, $level = 'NOTICE')

/**
* 获取写入流
* @method getStream
* @param [integer] $tag [日志级别]
* @method getFile
* @param [string] $tag [日志级别]
* @return [array] [description]
* @author NewFuture
*/
private static function getStream($tag)
private static function getFile($tag)
{
if (!isset(Logger::$_stream[$tag])) {
$files=&Logger::$_files;
if (!isset($files[$tag])) {
/*打开文件流*/
if (!$logdir=&Logger::$_dir) {
if (!isset($files['_dir'])) {
//日志目录
$logdir = Config::get('tempdir').DIRECTORY_SEPARATOR.'log';
if (is_dir($logdir)||mkdir($logdir, 0700, true)) {
date_default_timezone_set('PRC');
} else {
throw new \Exception('目录文件无法创建' . $logdir, 1);
$logdir = Config::get('runtime').DIRECTORY_SEPARATOR.'log';
if (!is_dir($logdir)) {
$oldmask=umask(0);
mkdir($logdir, Logger::$mode, true);
umask($oldmask);
}
date_default_timezone_set('PRC');
$files['_dir']=$logdir.DIRECTORY_SEPARATOR . date('y-m-d-');
}
//打开日志文件
$file = $logdir . DIRECTORY_SEPARATOR . date('y-m-d-') . $tag . '.log';
if (!Logger::$_stream[$tag] = fopen($file, 'a')) {
throw new \Exception('Cannot open to log file: ' . $file);

$file = $files['_dir'] . $tag . '.log';
//检查权限
if (($mode= @fileperms($file))&&(($mode&Logger::$mode) !== Logger::$mode)) {
chmod($file, Logger::$mode);
clearstatcache();
}
$files[$tag] = $file ;
}
return Logger::$_stream[$tag];
return $files[$tag];
}


Expand Down
16 changes: 7 additions & 9 deletions library/Storage/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
class File
{

public static $mode = 0700; //文件权限
protected $_dir = null; //文件目录
protected $_serialized = false; //是否序列化存取

Expand All @@ -35,7 +35,7 @@ public function set($name, $value)
}

$filename = $this->_dir . $name . '.php';
return file_put_contents($filename, $value);
return file_put_contents($filename, $value)&&chmod($filename, File::$mode);
}

/**
Expand Down Expand Up @@ -101,14 +101,12 @@ public function flush()
*/
public function __construct($dir, $serialized = false)
{
$dir .= DIRECTORY_SEPARATOR;
if (is_dir($dir)||mkdir($dir, 0700, true)) {
$this->_dir = $dir;
} else {
$msg='无法创建目录:'.$dir;
Log::write($msg, 'EEROR');
throw new \Exception($msg);
if (!is_dir($dir)) {
$oldmask=umask(0);
mkdir($dir, File::$mode, true);
umask($oldmask);
}
$this->_dir = $dir.DIRECTORY_SEPARATOR;
$this->_serialized = $serialized;
}
}

0 comments on commit a38752b

Please sign in to comment.