携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第22天,点击查看活动详情 >> 希望大家可以帮忙点个赞,谢谢!
前言
今天的工作挺忙的,又要接接口,又要修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的实现。