Print Log with timestamp

本文介绍了一个简单的Java方法,用于将带有时间戳的日志信息写入指定的PrintWriter对象。该方法首先检查传入的PrintWriter对象是否为空,然后获取当前日期和时间,并使用预定义的日期格式进行格式化。最后,它将格式化的时间戳和日志信息一起打印到缓冲区。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

	/*
	 * Print Log with timestamp.
	 */
	public void PrintLog(PrintWriter logBuffer, String line){
		if(logBuffer == null){
			return;
		}
		java.util.Date datetime = new java.util.Date();
		SimpleDateFormat DF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		logBuffer.println("[" + DF.format(datetime) + "]" + line);
	}

这个是完整代码,根据这个代码。生成log.txt,代码存在错误可以修改代码,如果没有必须根据解析的文件格式生成对应的txt。 from datetime import datetime, timedelta def parse_log_line(line): try: parts = line.split() timestamp = datetime.strptime(parts[2], '%H:%M:%S.%f') event = parts[3] return timestamp, event except (IndexError, ValueError): print(f"Invalid log line: {line.strip()}") return None, None def calculate_statistics(log_file): total_log_time = timedelta() total_rrc_connected_time = timedelta() rrc_connection_count = 0 total_rrc_idle_time = timedelta() last_release_time = None first_timestamp = None last_timestamp = None with open(log_file, 'r') as file: lines = file.readlines() for line in lines: timestamp, _ = parse_log_line(line) if timestamp: if not first_timestamp: first_timestamp = timestamp last_timestamp = timestamp # Check if first_timestamp and last_timestamp are not None before comparing if first_timestamp and last_timestamp: # 处理跨越24:00的情况 if last_timestamp < first_timestamp: last_timestamp += timedelta(days=1) total_log_time = last_timestamp - first_timestamp for line in lines: timestamp, event = parse_log_line(line) if event == 'UL_CCCH / RRC Setup Req': rrc_connection_count += 1 if last_release_time: total_rrc_idle_time += timestamp - last_release_time elif event == 'DL_DCCH / RRC Release': if last_release_time: total_rrc_connected_time += timestamp - last_release_time last_release_time = timestamp avg_rrc_idle_time = total_rrc_idle_time / rrc_connection_count if rrc_connection_count > 0 else timedelta() return { 'total_log_time': total_log_time,
03-20
这个是完整代码,根据这个代码。生成log.txt,不要修改代码,只是根据解析的文件格式生成对应的txt。 def parse_log_line(line): try: parts = line.split() timestamp = datetime.strptime(parts[2], '%H:%M:%S.%f') event = parts[3] return timestamp, event except (IndexError, ValueError): print(f"Invalid log line: {line.strip()}") return None, None def calculate_statistics(log_file): total_log_time = timedelta() total_rrc_connected_time = timedelta() rrc_connection_count = 0 total_rrc_idle_time = timedelta() last_release_time = None first_timestamp = None last_timestamp = None with open(log_file, 'r') as file: lines = file.readlines() for i, line in enumerate(lines, start=1): timestamp, _ = parse_log_line(line) if timestamp: if not first_timestamp: first_timestamp = timestamp last_timestamp = timestamp if first_timestamp and last_timestamp: if last_timestamp < first_timestamp: last_timestamp += timedelta(days=1) total_log_time = last_timestamp - first_timestamp for line in lines: timestamp, event = parse_log_line(line) if event == 'UL_CCCH / RRC Setup Req': rrc_connection_count += 1 if last_release_time: total_rrc_idle_time += timestamp - last_release_time elif event == 'DL_DCCH / RRC Release': if last_release_time: total_rrc_connected_time += timestamp - last_release_time last_release_time = timestamp else: print("No valid timestamps found in the log file.") avg_rrc_idle_time = total_rrc_idle_time / rrc_connection_count if rrc_connection_count > 0 else timedelta() return { 'total_log_time': total_log_time, 'total_rrc_connected_time': total_rrc_connected_time,
03-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值