Problem Description
The console log output is truncated, and the latter part cannot be printed. For example, consider the following log (each line has a certain number of characters, with a total of 30 lines and 4500 characters). When using hilog for printing, the log is truncated (only up to line 24 is printed).
Background Knowledge
Logs can print a maximum number of bytes. Text exceeding this limit will be truncated. For details, see Constraints and Limitations.
Solution
Use the hilog encapsulated log utility class to check the length of logs. If the length exceeds a specific threshold (e.g., 1024 bytes), print the log in segments.
The code example is as follows:
import hilog from '@ohos.hilog';
class LogUtil {
private static instance: LogUtil;
private static DOMAIN: number = 0x0000;
private constructor() {
// Private constructor to prevent external instantiation
}
public static getInstance(): LogUtil {
if (!LogUtil.instance) {
LogUtil.instance = new LogUtil();
}
return LogUtil.instance;
}
// error and debug methods can follow the same structure as info
public info(logTag: string, content: string) {
const maxSize = 1024;
if (content.length <= maxSize) {
// If the length is within the limit, log directly
} else {
while (content.length > maxSize) {
// Loop to print in segments
let logContent = content.substring(0, maxSize);
content = content.replace(logContent, "");
hilog.info(LogUtil.DOMAIN, logTag, '%{public}s', logContent);
// Print the remaining log content
}
}
hilog.info(LogUtil.DOMAIN, logTag, '%{public}s', content);
}
}
export default LogUtil;
After modification, the log can be printed completely:


Top comments (0)