从源码与官方文档看之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的实现。

相关文章
|
1天前
|
人工智能 自然语言处理 安全
阿里云AI数智鉴密:AI 生成内容如何拿到一张"防篡改的身份证"
隐形水印 + C2PA签名:让AI生成内容“持证上岗”。
1088 0
|
10天前
|
人工智能 自然语言处理 安全
阿里云千问办公、Qoder Teams、Qoder CN区别与选择指南:模型能力、适用场景与最新活动参考
本文聚焦阿里云2026年推出的三款自研AI办公产品,清晰拆解千问办公、Qoder Teams、Qoder CN的差异化定位与能力边界:千问办公主打职场全场景提效,支持自然语言指令一键完成PPT生成、数据分析等高频办公任务;Qoder Teams面向程序员团队,深度整合AI代码生成、团队协同与企业知识库能力;Qoder CN则专为金融、政务等强合规场景打造,实现数据不出境与VPC私有化部署。文章同步给出分场景选型指南与最新活动定价,帮助不同类型的企业按需组合产品,实现业务岗、研发岗与强合规场景的AI能力全覆盖。
3622 3
阿里云千问办公、Qoder Teams、Qoder CN区别与选择指南:模型能力、适用场景与最新活动参考
|
22天前
|
人工智能 缓存 前端开发
DeepSeek Harness 首发实测 + 入门教程,夯爆了!梁神我错了
DeepSeek Harness + DeepSeek V4 Pro 项目实战保姆级教程!手把手带你从零安装开源 AI 编程工具,开发架构图、知识讲解网站、3D 网页游戏、全栈 AI 应用 4 个项目,覆盖运行模式选择、插件安装与开发,看看能不能对标 Claude。
13356 91
DeepSeek Harness 首发实测 + 入门教程,夯爆了!梁神我错了
|
15天前
|
Web App开发 人工智能 API
16 个超火的 DeepSeek Harness 插件,大肥鱼已经落后 N 个版本了。。。
DeepSeek Harness 精选插件推荐合集,从图片识别、浏览器操控、多 Agent 协作到手机远程控制,一口气带你看完 DSH 社区热门的十几个插件,覆盖技能扩展、UI 界面增强、整活玩法三大类,让你的鲸鱼变得更强。
1883 5
|
8天前
|
人工智能 监控 测试技术
Qwen3.8-Flash 来了,100万上下文、Agent、Coding 都加强了
8月26日,通义千问发布Qwen3.8-Flash-Next:125B参数、每Token仅激活6B,原生支持26万Token、可扩展至100万上下文;Coding、Agent与工具调用能力显著增强,面向真实软件工程任务,推动大模型从“回答问题”迈向“完成工作”。
|
11天前
|
人工智能 Linux iOS开发
Ollama使用教程:Ollama官网下载、Ollama本地部署大模型(2026最新)
Ollama 是一款免费开源的本地大模型运行工具,支持在 Windows/macOS/Linux 上离线运行 Qwen、DeepSeek、Llama 等主流开源模型,数据不出本机、隐私安全。提供 OpenAI 兼容 API,命令行一键拉取/运行/管理模型,无需联网,无调用限制,是开发者与 AI 爱好者部署本地 AI 助手的理想选择。(239 字)
|
16天前
|
人工智能 Java BI
【AI】DeepSeek Harness 安装、运行、管理插件
本文介绍了如何运行DeepSeek开源的Agent框架DeepSeek Harness(dsh)。主要内容包括:使用nvm安装适配的Node版本;通过代理加速克隆GitHub源码;使用pnpm安装依赖并启动项目;配置DeepSeek API Token;安装扩展功能的插件。该框架自带Web界面,支持模型适配、文件编辑等插件化功能
2102 1