从源码与官方文档看之Handle篇(四)

简介: 今天的工作挺忙的,又要接接口,又要修Bug,还要完成需求。晚上家里又有一些事情忙活,所以我们废话不多说,直接进正题。

正篇

首先我们接着看下一个方法setFormatter:

/**
 * Set a <tt>Formatter</tt>.  This <tt>Formatter</tt> will be used
 * to format <tt>LogRecords</tt> for this <tt>Handler</tt>.
 * <p>
 * Some <tt>Handlers</tt> may not use <tt>Formatters</tt>, in
 * which case the <tt>Formatter</tt> will be remembered, but not used.
 * <p>
 * @param newFormatter the <tt>Formatter</tt> to use (may not be null)
 * @exception  SecurityException  if a security manager exists and if
 *             the caller does not have <tt>LoggingPermission("control")</tt>.
 */
public synchronized void setFormatter(Formatter newFormatter) throws SecurityException {
    checkPermission();
    // Check for a null pointer:
    newFormatter.getClass();
    formatter = newFormatter;
}

从翻译看,大致是说这个方法用来设置格式化,支持格式化日志数据。 此 Formatter 将用于为此 Handler 格式化 LogRecords对象。而一些Handle可能不使用Formatters,在这种情况下会被Formatter记住,但不会被使用。通常每个Handler中都会有一个Formatter引用,Formatter可以将LogRecord对象转换为一个string字符串。

/**
 * Return the <tt>Formatter</tt> for this <tt>Handler</tt>.
 * @return the <tt>Formatter</tt> (may be null).
 */
public Formatter getFormatter() {
    return formatter;
}

这个是接受formatter的方法,说到这,不得不提一下,Handle和Formatter类是两个抽象类,它们可以分别独立的变化(有不同的子类);而Handle类中包含对Formatter类的引用。Formatter支持格式化日志数据;通常每个Handler中都会有一个Formatter引用,Formatter可以将LogRecord对象转换为一个string字符串。

/**
 * Set the character encoding used by this <tt>Handler</tt>.
 * <p>
 * The encoding should be set before any <tt>LogRecords</tt> are written
 * to the <tt>Handler</tt>.
 *
 * @param encoding  The name of a supported character encoding.
 *        May be null, to indicate the default platform encoding.
 * @exception  SecurityException  if a security manager exists and if
 *             the caller does not have <tt>LoggingPermission("control")</tt>.
 * @exception  UnsupportedEncodingException if the named encoding is
 *          not supported.
 */
public synchronized void setEncoding(String encoding)
                    throws SecurityException, java.io.UnsupportedEncodingException {
    checkPermission();
    if (encoding != null) {
        try {
            if(!java.nio.charset.Charset.isSupported(encoding)) {
                throw new UnsupportedEncodingException(encoding);
            }
        } catch (java.nio.charset.IllegalCharsetNameException e) {
            throw new UnsupportedEncodingException(encoding);
        }
    }
    this.encoding = encoding;
}
/**
 * Return the character encoding for this <tt>Handler</tt>.
 *
 * @return  The encoding name.  May be null, which indicates the
 *          default encoding should be used.
 */
public String getEncoding() {
    return encoding;
}

setEncoding方法的大意是:

本方法可以设置 Handler使用的字符编码。 但应该在将任何 LogRecord 对象写入 Handler 之前设置编码。

方法的参数:

encoding – 支持的字符编码的名称。 可以为 null,表示默认的平台编码。

错误的抛出:

SecurityException – 如果存在安全管理器且没调用LoggingPermission("control")。

UnsupportedEncodingException – 如果不支持命名编码。

而getEncoding()方法是返回Handle的字符编码。 (未完待续)

总结

这些在安卓开发日常中还是不大容易接触到的,我们会继续往下看,然后去看Handle的实现。

相关文章
|
SQL 存储 消息中间件
汽车之家:基于 Flink + Iceberg 的湖仓一体架构实践
由汽车之家实时计算平台负责人邸星星在 4 月 17 日上海站 Meetup 分享的,基于 Flink + Iceberg 的湖仓一体架构实践。
汽车之家:基于 Flink + Iceberg 的湖仓一体架构实践
|
11月前
|
自然语言处理 搜索推荐 前端开发
语镜VocaMirror——基于sensevoice、cosyvoice和qwen模型实现与“自身声音”对话
语镜 VocaMirror 是一个创新的对话系统,灵感来源于汤姆猫游戏和亲人语音克隆项目,旨在让用户与自己的声音进行对话。系统融合了语音识别、自然语言处理及个性化语音合成技术,提供趣味互动、心理治疗辅助及多功能扩展等应用。用户可通过 Gradio 界面轻松使用,实现语音转文本、对话生成及个性化语音回复等功能。
854 4
语镜VocaMirror——基于sensevoice、cosyvoice和qwen模型实现与“自身声音”对话
|
安全 Python
告别低效编程!Python线程与进程并发技术详解,让你的代码飞起来!
【7月更文挑战第9天】Python并发编程提升效率:**理解并发与并行,线程借助`threading`模块处理IO密集型任务,受限于GIL;进程用`multiprocessing`实现并行,绕过GIL限制。示例展示线程和进程创建及同步。选择合适模型,注意线程安全,利用多核,优化性能,实现高效并发编程。
216 3
|
前端开发 JavaScript API
网页自动提交Form表单的方法
在数字化时代,自动化任务如网页自动提交Form表单,能大幅提升效率。这涉及自动填写注册信息等场景。本文概述了多种实现方式:JavaScript可直接在前端自动填充并提交;Python结合Selenium模拟真实用户操作;AOKSend作为API工具发送表单数据;第三方工具如iMacros、AutoHotkey和Zapier提供非编程自动化选项。根据需求选择合适方法,可显著提升工作效能,减少重复性劳动。
|
SQL 开发框架 .NET
一个超级大的文件如何更快读
# 一个超级大的文件如何更快读 问题起因 ![](https://img2023.cnblogs.com/blog/2415052/202306/2415052-20230608110517159-989018809.png) 一个有千万的数据的txt文件如何发挥IO的全部性能更快的读和写。 ## 方案一 使用ChatGPT4的方案 在C#中,我们可以使用多线程来处理大量的数据并将其写入数据库。在处理大数据时,我们需要将任务分解为多个子任务,这样我们可以在不同的线程中并行执行它们以提高性能。 这里是一种可能的解决方案,使用了`Task Parallel Library (TPL
224 0
一个超级大的文件如何更快读
|
弹性计算 人工智能 Linux
OS Copilot试用感受
### 开发者速览:阿里云OS Copilot 探索阿里云Linux体验,程序员借助OS Copilot提升效率。从云起实践平台开始,跟随教程启动ECS,安装Copilot,配置密钥,辅助编程及命令执行。反馈指出,系统集成强,命令辅助优秀,评分9分。建议增强交互界面,增加报错分析与自动修复功能,并期待与ACK、ECS等更紧密集成。多行编辑器和代码高亮执行是改进方向。
126 0
|
存储 算法 Java
Java数据结构与算法分析(三)链表(单链表、双链表、环形链表)
通过前篇文章《[数组](https://blog.csdn.net/gozhuyinglong/article/details/109702860)》了解到数组的存储结构是一块连续的内存,插入和删除元素时其每个部分都有可能整体移动。为了避免这样的线性开销,我们需要保证数据可以不连续存储。本篇介绍另一种数据结构:链表。
401 0
软件产品竞争分析
在开始一个产品的开发工作之前,我们需要制定产品的战略,产品战略中很关键的一点是要找到产品的竞争对手,并对其进行分析。通过对竞争对手的分析,有针对性的调整企业自己的产品。
2705 0
|
缓存 JavaScript 前端开发
x5开源库后续知识点
目录介绍 01.基础使用目录介绍 1.0.1 常用的基础介绍 1.0.2 Android调用Js 1.0.3 Js调用Android 1.0.4 WebView.loadUrl(url)流程 1.
2459 0
|
Prometheus 监控 Cloud Native
使用 Prometheus 监控 Docker 容器
本文讲的是使用 Prometheus 监控 Docker 容器,【编者的话】Prometheus支持深度监控Docker容器的资源和运行特性,多维度查询,聚合Docker监控数据,作者认为Prometheus是最适合基于容器架构的监控系统,其特性是高纬度数据模型和灵活的查询语言 。
4289 0