我使用的是使用logging
模块的Python库。但是,我创建了自己的log
函数,我的脚本在内部使用它。
这里是我想要使用的日志记录功能:
def log(name, content, swtch : bool = None, time = None):
time = time or datetime.now(pytz.timezone('US/Pacific'))
if swtch == True or swtch == None:
toPrint = '{1.RED}{2}/{3}/{4} {5}:{6}:{7}:{8} {9} {0.BRIGHT}{1.GREEN}{10} {0.RESET_ALL}{11}{0.RESET_ALL}'.format(
Style,
Fore,
str(time.month).zfill(2),
str(time.day).zfill(2),
str(time.year)[2:],
str(time.hour % 12).zfill(2),
str(time.minute).zfill(2),
str(time.second).zfill(2),
str(int(time.microsecond / 1000)).zfill(3),
'AM' if time.hour < 12 else 'PM',
name,
content
)
print(toPrint)
log_txt = ''
if swtch == False or swtch == None:
file = open('log.txt', 'r')
log_txt = file.read()
file.close()
with open('log.txt', 'w') as file:
text = '{0}/{1}/{2} {3}:{4}:{5}:{6} {7} {8} {9}'.format(
str(time.month).zfill(2),
str(time.day).zfill(2),
str(time.year)[2:],
str(time.hour % 12).zfill(2),
str(time.minute).zfill(2),
str(time.second).zfill(2),
str(int(time.microsecond / 1000)).zfill(3),
'AM' if time.hour < 12 else 'PM',
name,
content
)
file.write(log_txt + text + '\n')
让我们假设有一个名为some_logger
的记录器。
import logging
log = logging.getLogger('some_logger')
有没有办法,而不是打印到stdout,而是将位置参数(表示日志信息)传递给另一个函数? (澄清:将使用所需的args调用log
的函数)
答案 0 :(得分:1)
我发现你可以使用类似Handler
对象的东西来做到这一点。您需要定义一个实现emit
的子类。有关基本设置,请参阅以下示例:
<script type='text/javascript' src='http://www.google.com/jsapi'></script>
<script type='text/javascript'>google.load('visualization', '1', {'packages': ['geochart']});
google.setOnLoadCallback(drawVisualization);
function drawVisualization()
{
var query = new google.visualization.Query('https://docs.google.com/spreadsheets/d/1dTfxVvfDKn6iXn4W_m7HJ_86JOGNDsxYSSaXipEo0vM/edit#gid=0');
// query.setQuery('select A,B,C');
query.send(handleQueryResponseTR);
}
function handleQueryResponseTR(response) {
if (response.isError()) {
alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
var options = {
backgroundColor: {fill:'#FFFFFF',stroke:'#FFFFFF' ,strokeWidth:0 },
colorAxis: {colors: ['#D95F0E','#FEC44F','#FEC44F','#FEC44F','#FEC44F','#FEC44F','#FEC44F','#FEC44F','#FEC44F','#FFF7BC','#FFF7BC','#FFF7BC','#FFF7BC',]},
backgroundColor: {fill:'#FFFFFF',stroke:'#FFFFFF' ,strokeWidth:0 },
datalessRegionColor: '#F5F0E7',
displayMode: 'regions',
enableRegionInteractivity: 'true',
resolution: 'countries',
sizeAxis: {minValue: 1, maxValue:1,minSize:10, maxSize: 10},
region:'world',
keepAspectRatio: true,
width:800,
height:600,
tooltip: {isHtml:'true',textStyle: {color: '#444444'}, trigger:'focus'}
};
var data = response.getDataTable();
var view = new google.visualization.DataView(data);
view.setColumns([0,{
type:'string',
label : 'num of',
calc: function (dt, row) {
return {
v: dt.getValue(row, 1),
f: dt.getFormattedValue(row, 1) + ': (' + dt.getFormattedValue(row, 2) + ' .)'
}
}
}]);
var chart = new google.visualization.GeoChart(document.getElementById('visualization'));
chart.draw(view, options);
}
</script>
<div id='visualization'></div>