001/* 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2025, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v1.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014 015package ch.qos.logback.core.model.processor; 016 017import ch.qos.logback.core.Context; 018import ch.qos.logback.core.FileAppender; 019import ch.qos.logback.core.model.AppenderModel; 020import ch.qos.logback.core.model.ImplicitModel; 021import ch.qos.logback.core.model.Model; 022import ch.qos.logback.core.rolling.RollingFileAppender; 023import ch.qos.logback.core.rolling.helper.FileNamePattern; 024 025import java.util.HashMap; 026import java.util.List; 027import java.util.Map; 028import java.util.Optional; 029import java.util.function.Supplier; 030import java.util.stream.Collectors; 031import java.util.stream.Stream; 032 033@PhaseIndicator(phase = ProcessingPhase.DEPENDENCY_ANALYSIS) 034public class FileCollisionAnalyser extends ModelHandlerBase { 035 036 public FileCollisionAnalyser(Context context) { 037 super(context); 038 } 039 040 @Override 041 protected Class<AppenderModel> getSupportedModelClass() { 042 return AppenderModel.class; 043 } 044 045 046 @Override 047 public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException { 048 AppenderModel appenderModel = (AppenderModel) model; 049 050 String originalClassName = appenderModel.getClassName(); 051 String className = mic.getImport(originalClassName); 052 053 String appenderName = appenderModel.getName(); 054 055 if (!fileAppenderOrRollingFileAppender(className)) { 056 return; 057 } 058 059 String tagName0 = "file"; 060 checkForCollisions(mic, MapKey.FILE_COLLISION_MAP_KEY, appenderModel, appenderName, tagName0); 061 062 String tagName1 = "fileNamePattern"; 063 checkForCollisions(mic, MapKey.RFA_FILENAME_COLLISION_MAP, appenderModel, appenderName, tagName1); 064 } 065 066 private static boolean fileAppenderOrRollingFileAppender(String className) { 067 return FileAppender.class.getName().equals(className) || RollingFileAppender.class.getName().equals(className); 068 } 069 070 071 boolean tagPredicate(Model model, String tagName) { 072 return (model instanceof ImplicitModel) && tagName.equals(model.getTag()); 073 } 074 075 enum MapKey { 076 FILE_COLLISION_MAP_KEY, RFA_FILENAME_COLLISION_MAP 077 } 078 079 private void checkForCollisions(ModelInterpretationContext mic, MapKey mapKey, AppenderModel appenderModel, String appenderName, final String tagName) { 080 081 082 Stream<Model> streamLevel1 = appenderModel.getSubModels().stream(); 083 Stream<Model> streamLevel2 = appenderModel.getSubModels().stream().flatMap(child -> child.getSubModels().stream()); 084 085 List<Model> matchingModels = Stream.concat(streamLevel1, streamLevel2).filter(m -> tagPredicate(m, tagName)).collect(Collectors.toList()); 086 087 //List<Model> matchingModels = appenderModel.getSubModels().stream().filter(m -> tagPredicate(m, tagName)).collect(Collectors.toList()); 088 089 if(!matchingModels.isEmpty()) { 090 ImplicitModel implicitModel = (ImplicitModel) matchingModels.get(0); 091 String bodyValue = mic.subst(implicitModel.getBodyText()); 092 093 094 Map<String, String> faileCollisionMap = getCollisionMapByKey(mic, mapKey); 095 096 Optional<Map.Entry<String, String>> collision = faileCollisionMap.entrySet() 097 .stream() 098 .filter(entry -> bodyValue.equals(entry.getValue())) 099 .findFirst(); 100 101 if (collision.isPresent()) { 102 addErrorForCollision(tagName, appenderName, collision.get().getKey(), bodyValue); 103 appenderModel.markAsHandled(); 104 appenderModel.deepMarkAsSkipped(); 105 } else { 106 // add to collision map if and only if no collision detected 107 // reasoning: single entry is as effective as multiple entries for collision detection 108 faileCollisionMap.put(appenderName, bodyValue); 109 } 110 } 111 } 112 113 private Map<String, String> getCollisionMapByKey(ModelInterpretationContext mic, MapKey mapKey) { 114 Map<String, String> map = (Map<String, String>) mic.getObjectMap().get(mapKey.name()); 115 if(map == null) { 116 map = new HashMap<>(); 117 mic.getObjectMap().put(mapKey.name(), map); 118 } 119 return map; 120 } 121 122 123 static public final String COLLISION_DETECTED = "Collision detected. Skipping initialization of appender named [%s]"; 124 static public final String COLLISION_MESSAGE = "In appender [%s] option '%s' has the same value '%s' as that set for appender [%s] defined earlier"; 125 private void addErrorForCollision(String optionName, String appenderName, String previousAppenderName, String optionValue) { 126 addError(String.format(COLLISION_DETECTED, appenderName)); 127 addError(String.format(COLLISION_MESSAGE, appenderName, optionName, optionValue, previousAppenderName)); 128 } 129}