001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2026 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018/////////////////////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle; 021 022import java.util.Locale; 023 024import com.puppycrawl.tools.checkstyle.api.AuditEvent; 025import com.puppycrawl.tools.checkstyle.api.SeverityLevel; 026 027/** 028 * Represents the default formatter for log message. 029 * Default log message format is: 030 * [SEVERITY LEVEL] filePath:lineNo:columnNo: message. [CheckName] 031 * When the module id of the message has been set, the format is: 032 * [SEVERITY LEVEL] filePath:lineNo:columnNo: message. [ModuleId] 033 */ 034public class AuditEventDefaultFormatter implements AuditEventFormatter { 035 036 /** Length of all separators. */ 037 private static final int LENGTH_OF_ALL_SEPARATORS = 10; 038 039 /** Suffix of module names like FooCheck. */ 040 private static final String SUFFIX = "Check"; 041 042 /** 043 * Creates a new {@code AuditEventDefaultFormatter} instance. 044 */ 045 public AuditEventDefaultFormatter() { 046 // no code by default 047 } 048 049 @Override 050 public String format(AuditEvent event) { 051 final String fileName = event.getFileName(); 052 final String message = event.getMessage(); 053 054 final SeverityLevel severityLevel = event.getSeverityLevel(); 055 final String severityLevelName; 056 if (severityLevel == SeverityLevel.WARNING) { 057 // We change the name of severity level intentionally 058 // to shorten the length of the log message. 059 severityLevelName = "WARN"; 060 } 061 else { 062 severityLevelName = severityLevel.getName().toUpperCase(Locale.US); 063 } 064 065 final StringBuilder sb = initStringBuilderWithOptimalBuffer(event, severityLevelName); 066 067 sb.append('[').append(severityLevelName).append("] ") 068 .append(fileName).append(':').append(event.getLine()); 069 if (event.getColumn() > 0) { 070 sb.append(':').append(event.getColumn()); 071 } 072 sb.append(": ").append(message).append(" ["); 073 if (event.getModuleId() == null) { 074 final String checkShortName = getCheckShortName(event); 075 sb.append(checkShortName); 076 } 077 else { 078 sb.append(event.getModuleId()); 079 } 080 sb.append(']'); 081 082 return sb.toString(); 083 } 084 085 /** 086 * Returns the StringBuilder that should avoid StringBuffer.expandCapacity. 087 * bufferLength = fileNameLength + messageLength + lengthOfAllSeparators + 088 * + severityNameLength + checkNameLength. 089 * Method is excluded from pitest validation. 090 * 091 * @param event audit event. 092 * @param severityLevelName severity level name. 093 * @return optimal StringBuilder. 094 */ 095 private static StringBuilder initStringBuilderWithOptimalBuffer(AuditEvent event, 096 String severityLevelName) { 097 final int bufLen = LENGTH_OF_ALL_SEPARATORS + event.getFileName().length() 098 + event.getMessage().length() + severityLevelName.length() 099 + getCheckShortName(event).length(); 100 return new StringBuilder(bufLen); 101 } 102 103 /** 104 * Returns check name without 'Check' suffix. 105 * 106 * @param event audit event. 107 * @return check name without 'Check' suffix. 108 */ 109 private static String getCheckShortName(AuditEvent event) { 110 final String checkFullName = event.getSourceName(); 111 String checkShortName = checkFullName.substring(checkFullName.lastIndexOf('.') + 1); 112 if (checkShortName.endsWith(SUFFIX)) { 113 final int endIndex = checkShortName.length() - SUFFIX.length(); 114 checkShortName = checkShortName.substring(0, endIndex); 115 } 116 return checkShortName; 117 } 118 119}