33import com .automationanywhere .toolchain .runtime .session .CloseableSessionObject ;
44import org .apache .commons .io .FilenameUtils ;
55import org .apache .logging .log4j .Level ;
6- import org .apache .logging .log4j .LogManager ;
76import org .apache .logging .log4j .Logger ;
8- import org .apache .logging .log4j .core .Appender ;
97import org .apache .logging .log4j .core .LoggerContext ;
10- import org .apache .logging .log4j .core .appender .RollingFileAppender ;
11- import org .apache .logging .log4j .core .config .Configuration ;
12- import org .apache .logging .log4j .core .config .ConfigurationFactory ;
13- import org .apache .logging .log4j .core .config .ConfigurationSource ;
14- import org .apache .logging .log4j .core .config .DefaultConfiguration ;
158import org .apache .logging .log4j .core .config .builder .api .AppenderComponentBuilder ;
169import org .apache .logging .log4j .core .config .builder .api .ConfigurationBuilder ;
1710import org .apache .logging .log4j .core .config .builder .api .LayoutComponentBuilder ;
@@ -39,6 +32,7 @@ public class CustomLogger implements CloseableSessionObject {
3932 private final String loggerId ;
4033 private final LoggerContext loggerContext ;
4134 private final Map <Level , String > screenshotFolderPaths ;
35+ private final Map <Level , String > variablesFolderPaths ;
4236
4337 // Constructor for a single log file for all levels
4438 public CustomLogger (String loggerName , String logFilePath , long sizeLimitMB ) throws IOException {
@@ -47,15 +41,22 @@ public CustomLogger(String loggerName, String logFilePath, long sizeLimitMB) thr
4741 // Create screenshot folder at the same location as log file
4842 String baseDir = FilenameUtils .getFullPath (logFilePath );
4943 String screenshotFolder = baseDir + "screenshots" ;
44+ String variablesFolder = baseDir + "variables" ;
5045
5146 this .screenshotFolderPaths = new HashMap <>();
52- // Use the same screenshot folder for all levels when using a combined log file
47+ this .variablesFolderPaths = new HashMap <>();
48+
49+ // Use the same folder for all levels when using a combined log file
5350 this .screenshotFolderPaths .put (Level .INFO , screenshotFolder );
5451 this .screenshotFolderPaths .put (Level .WARN , screenshotFolder );
5552 this .screenshotFolderPaths .put (Level .ERROR , screenshotFolder );
5653
57- // Create screenshot directory
58- createScreenshotDirectories ();
54+ this .variablesFolderPaths .put (Level .INFO , variablesFolder );
55+ this .variablesFolderPaths .put (Level .WARN , variablesFolder );
56+ this .variablesFolderPaths .put (Level .ERROR , variablesFolder );
57+
58+ // Create directories
59+ createDirectories ();
5960
6061 // Create a unique logger context for this instance
6162 LoggerContext context = createNewLoggerContext ();
@@ -64,7 +65,8 @@ public CustomLogger(String loggerName, String logFilePath, long sizeLimitMB) thr
6465 ConfigurationBuilder <BuiltConfiguration > builder = new DefaultConfigurationBuilder ();
6566 setupLoggerConfiguration (builder );
6667
67- AppenderComponentBuilder appenderBuilder = getCustomAppenderBuilder (builder , "COMBINED_" + loggerId , logFilePath ,
68+ AppenderComponentBuilder appenderBuilder = getCustomAppenderBuilder (builder , "COMBINED_" + loggerId ,
69+ logFilePath ,
6870 sizeLimitMB );
6971 builder .add (appenderBuilder );
7072 builder .add (builder .newLogger (loggerName , Level .INFO )
@@ -84,6 +86,20 @@ public CustomLogger(String loggerName, String logFilePath, long sizeLimitMB) thr
8486 this .logger = context .getLogger (loggerName );
8587 }
8688
89+ private void createDirectories () throws IOException {
90+ // Create screenshot directories
91+ for (String path : screenshotFolderPaths .values ()) {
92+ Path directoryPath = Paths .get (path );
93+ Files .createDirectories (directoryPath );
94+ }
95+
96+ // Create variables directories
97+ for (String path : variablesFolderPaths .values ()) {
98+ Path directoryPath = Paths .get (path );
99+ Files .createDirectories (directoryPath );
100+ }
101+ }
102+
87103 private LoggerContext createNewLoggerContext () {
88104 // Creating a completely separate LoggerContext
89105 return new LoggerContext ("Context-" + loggerId );
@@ -122,17 +138,20 @@ private AppenderComponentBuilder getCustomAppenderBuilder(ConfigurationBuilder<B
122138 public CustomLogger (String loggerName , Map <Level , String > levelFilePathMap , long sizeLimitMB ) throws IOException {
123139 this .loggerId = UUID .randomUUID ().toString ();
124140 this .screenshotFolderPaths = new HashMap <>();
141+ this .variablesFolderPaths = new HashMap <>();
125142
126- // Create a screenshot folder alongside each log file
143+ // Create a screenshot and variables folder alongside each log file
127144 for (Map .Entry <Level , String > entry : levelFilePathMap .entrySet ()) {
128145 Level level = entry .getKey ();
129146 String filePath = entry .getValue ();
130147 String baseDir = FilenameUtils .getFullPath (filePath );
148+
131149 this .screenshotFolderPaths .put (level , baseDir + "screenshots" );
150+ this .variablesFolderPaths .put (level , baseDir + "variables" );
132151 }
133152
134- // Create all screenshot directories
135- createScreenshotDirectories ();
153+ // Create all directories
154+ createDirectories ();
136155
137156 // Create a unique logger context for this instance
138157 LoggerContext context = createNewLoggerContext ();
@@ -176,14 +195,6 @@ public CustomLogger(String loggerName, Map<Level, String> levelFilePathMap, long
176195 this .logger = context .getLogger (loggerName );
177196 }
178197
179- private void createScreenshotDirectories () throws IOException {
180- // Create unique directories only (remove duplicates)
181- for (String path : screenshotFolderPaths .values ()) {
182- Path directoryPath = Paths .get (path );
183- Files .createDirectories (directoryPath );
184- }
185- }
186-
187198 public Logger getLogger () {
188199 return logger ;
189200 }
@@ -210,9 +221,30 @@ public String getScreenshotFolderPath(Level level) {
210221
211222 /**
212223 * Returns the screenshot folder path for the INFO level
224+ *
213225 * @return default screenshot folder path (INFO level)
214226 */
215227 public String getScreenshotFolderPath () {
216228 return screenshotFolderPaths .get (Level .INFO );
217229 }
230+
231+ /**
232+ * Returns the variables folder path for the specified log level
233+ *
234+ * @param level Log level
235+ *
236+ * @return variables folder path for the specified level
237+ */
238+ public String getVariablesFolderPath (Level level ) {
239+ return variablesFolderPaths .getOrDefault (level , variablesFolderPaths .get (Level .INFO ));
240+ }
241+
242+ /**
243+ * Returns the variables folder path for the INFO level
244+ *
245+ * @return default variables folder path (INFO level)
246+ */
247+ public String getVariablesFolderPath () {
248+ return variablesFolderPaths .get (Level .INFO );
249+ }
218250}
0 commit comments