使用下面的配置,我的日志文件将被称为'test-debug.log',并且每次运行脚本时它都会无限增长.我只是希望这个日志文件包含最近一次运行脚本的日志记录.应该在重新开始之前删除日志.
我怎么做?
logger = logging.getLogger('test') #Create a log with the same name as the script that created it
logger.setLevel('DEBUG')
#Create handlers and set their logging level
filehandler_dbg = logging.FileHandler(logger.name + '-debug.log')
filehandler_dbg.setLevel('DEBUG')
#Create custom formats of the logrecord fit for both the logfile and the console
streamformatter = logging.Formatter(fmt='%(levelname)s:\t%(threadName)s:\t%(funcName)s:\t\t%(message)s', datefmt='%H:%M:%S') #We only want to see certain parts of the message
#Apply formatters to handlers
filehandler_dbg.setFormatter(streamformatter)
#Add handlers to logger
logger.addHandler(filehandler_dbg)
Run Code Online (Sandbox Code Playgroud) 我使用的是Windows 7和python 2.7.我想将日志文件大小限制为5MB.我的应用程序在启动时写入日志文件,然后应用程序终止.当我的应用程序再次启动时,它将写入相同的日志文件.因此app不会持续运行.应用程序启动,处理和终止.
我的日志记录代码是:
import logging
import logging.handlers
logging.basicConfig(filename=logfile.log, level="info", format='%(asctime)s %(levelname)s %(funcName)s(%(lineno)d) %(message)s')
logging.info("*************************************************")
Run Code Online (Sandbox Code Playgroud)
我尝试使用RotatingFileHandler但它没有用
logging.handlers.RotatingFileHandler(logFile, mode='a', maxBytes=5*1024*1024, backupCount=2, encoding=None, delay=0)
Run Code Online (Sandbox Code Playgroud)
那么,我如何在python中强制执行文件大小限制?
首先,一个简单的测试代码:
package javaapplication23;
import java.io.IOException;
import java.util.logging.FileHandler;
public class JavaApplication23 {
public static void main(String[] args) throws IOException {
new FileHandler("./test_%u_%g.log", 10000, 100, true);
}
}
Run Code Online (Sandbox Code Playgroud)
此测试代码仅使用Java 7创建一个文件"test_0_0.log",无论我多久运行一次该程序.这是预期的行为,因为构造函数中的append参数设置为true.
但是如果我在Java 8中运行此示例,则每次运行都会创建一个新文件(test_0_0.log,test_0_1.log,test_0_2.log,...).我认为这是一个错误.
Imho,Java的相关变化是这样的:
@@ -413,18 +428,18 @@
// object. Try again.
continue;
}
- FileChannel fc;
+
try {
- lockStream = new FileOutputStream(lockFileName);
- fc = lockStream.getChannel();
- } catch (IOException ix) {
- // We got an IOException while trying to open the file.
- // Try the next file. …Run Code Online (Sandbox Code Playgroud) 我正在编写一些使用python logging系统的代码.我们的想法是,如果LOG尚未存在,则创建日志,但如果存在,则获取日志并继续记录到该文件.这是我的代码:
import logging
import os
log_filename='Transactions.log')
if os.path.isfile(log_filename)!=True:
LOG = logging.getLogger('log_filename')
LOG.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler('log_filename')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter and add it to the handlers
formatter = logging.Formatter('-->%(asctime)s - %(name)s:%(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# add the handlers to the logger
LOG.addHandler(fh)
LOG.addHandler(ch)
else:
LOG=logging.getLogger()
Run Code Online (Sandbox Code Playgroud)
我怀疑问题出在我的else阻止,但我不知道如何解决.任何人都可以对这种情况有所了解.
首先,对不起,如果有人问我但找不到它.我正在从远程资源下载我的应用程序中的文档.下载文档后,我想为用户打开它.我想知道的是我如何检查他们是否有应用程序来处理Pdf或Tiff并在默认应用程序中启动它?
谢谢.
编辑
这是解决方案的一部分:
Intent viewDoc = new Intent(Intent.ACTION_VIEW);
viewDoc.setDataAndType(
Uri.fromFile(getFileStreamPath("test.pdf")),
"application/pdf");
PackageManager pm = getPackageManager();
List<ResolveInfo> apps =
pm.queryIntentActivities(viewDoc, PackageManager.MATCH_DEFAULT_ONLY);
if (apps.size() > 0)
startActivity(viewDoc);
Run Code Online (Sandbox Code Playgroud) 如果我正在编写一个Perl脚本,用STDOUT的副本覆盖STDERR,但我从不恢复文件句柄,那么在脚本执行结束时会发生什么?我找不到任何警告实际发生或不发生的事情.
我可能被误导了,但感谢你的耐心等待.
我在我的 python 源代码中使用记录器,并想在特定位置创建日志,但python 日志记录模块在默认位置创建日志文件,即从它执行的位置。
有没有办法改变这个默认位置?
下面是我的配置
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename='testGene.log, filemode='w')
Run Code Online (Sandbox Code Playgroud) 我有一个生成我的图像的ashx文件处理程序.
<img src="www.mywebsite.com/action/getimage.ashx?imageID=f8be4bf6&width=100&height=700&bgcolor=999" />
Run Code Online (Sandbox Code Playgroud)
一切正常.
现在,我想使用延迟加载.使用这个jquery延迟加载插件
所以我调整了我的html图像:
<img src="imageplaceholder.gif" original-data="www.mywebsite.com/action/getimage.ashx?imageID=f8be4bf6&width=100&height=700&bgcolor=999" />
Run Code Online (Sandbox Code Playgroud)
并添加了以下脚本:
$(function() {
$("img").lazyload();
});
Run Code Online (Sandbox Code Playgroud)
我注意到在谷歌Chrome devoloper工具的网络选项卡中,有两个调用此文件处理程序.
我在这里创建了一个测试小提琴:链接 如果您向下滚动此小提琴,您将在Google Chrome中加载图像时看到两个图像请求.在Firefox和IE中,这只能使用一个调用.
有什么方法可以避免这种行为吗?
更新:
在文件处理程序中设置以下标头:
[0] "Server" "Microsoft-IIS/7.5"
[1] "Set-Cookie" "lang=nl; expires=Tue, 04-Feb-2014 13:08:56 GMT; path=/"
Run Code Online (Sandbox Code Playgroud)
并且Response对象的Expires属性是:
context.Response.Expires = 0
Run Code Online (Sandbox Code Playgroud) 我在我的django应用程序中使用python日志记录.如果需要,连接到后端api的类会使用文件处理程序初始化此记录器.每次进行api调用时,类都会被实例化.我已经尝试确保每次都不添加其他处理程序,但是
lsof | grep my.log
Run Code Online (Sandbox Code Playgroud)
在我的日志文件中显示越来越多的处理程序,一段时间后我的服务器因此打开文件限制而失败.
self.logger = logging.getLogger("FPA")
try:
if self.logger.handlers[0].__class__.__name__=="FileHandler":
pass
except Exception, e:
print 'new filehandler added'+str(e)
ch = logging.FileHandler(FPA_LOG_TARGET)
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s - %(pathname)s @ line %(lineno)d")
ch.setFormatter(formatter)
self.logger.setLevel(logging.DEBUG)
self.logger.addHandler(ch)
Run Code Online (Sandbox Code Playgroud)
我意识到这可能不是最好的方法,但到目前为止我还没有在我的实现中发现错误.
目前我正在尝试打开一个名为“temperature.txt”的文本文件,我使用文件处理程序保存在我的桌面上,但是由于某种原因我无法让它工作。谁能告诉我我做错了什么。
#!/Python34/python
from math import *
fh = open('temperature.txt')
num_list = []
for num in fh:
num_list.append(int(num))
fh.close()
Run Code Online (Sandbox Code Playgroud)