-
Notifications
You must be signed in to change notification settings - Fork 4
/
LogSimulator.groovy
1053 lines (883 loc) · 37.1 KB
/
LogSimulator.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.util.StringTokenizer;
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
import java.xml.*;
import java.net.ServerSocket;
import java.util.logging.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
// this utility will either delete the nominated node from the API management logical OR
// approve a pending gateway join. The behaviour is dictated by the parameters
//public class LogSimulator
public class LogGenerator
{
final static String HELPMSG = "Parameters :\n" +
" 1. Expected: properties file e.g. tool.properties containing all the controls - \n if not defined then a default tool.properties will attempt to be loaded\n" +
" 2. Optional: simulated log messages e.g. data.txt - \n if provided this overrides the source file defined in the properties" +
"\n\nFull help available at https://github.com/mp3monster/LogGenerator \n\n";
final static String YPROPVAL = "y";
final static String YESPROPVAL = "yes";
final static String TPROPVAL = "t";
final static String TRUEPROPVAL = "true";
final static String LOOP = "REPEAT";
final static String JULCONFIG = "JULCONFIG";
final static String JULNAME = "JULName";
final static String SOURCESEPARATOR= "SOURCE-SEPARATOR";
final static String TARGETSEPARATOR= "TARGET-SEPARATOR";
final static String SOURCEFORMAT = "SOURCEFORMAT";
final static String TARGETFORMAT = "TARGETFORMAT";
final static String SOURCEFILE = "SOURCE";
final static String TARGETFILE = "TARGETFILE";
final static String TARGETIP="TARGETIP";
final static String TARGETPORT="TARGETPORT";
final static String TARGETDTG = "TARGETDTG";
final static String TARGETURL = "TARGETURL";
final static String SOURCEDTG = "SOURCEDTG";
final static String OUTTYPE = "OUTPUTTYPE";
final static String DEFAULTDELAYOFFSET= "DEFAULTTIMEOFFSET";
final static String CUSTOMOUTTYPE = "CUSTOMOUTPUT";
final static String DEFAULTLOGLEVEL = "DEFAULT-LOGLEVEL";
final static String ACCELERATOR = "ACCELERATEBY";
final static String CONSOLE = "console";
final static int UNKNOWNOUTPUT = -1;
final static int CONSOLEOUTPUT = 0;
final static String HTTP = "HTTP";
final static int HTTPOUTPUT = 1;
final static String FILE = "file";
final static int FILEOUTPUT = 2;
final static String TCPOUT="TCP";
final static int TCPOUTPUT = 3;
final static int JUL = 4;
final static String JULOUT="JUL";
final static int SYSSTD = 5;
final static String SYSSTDOUT="STDOUT";
final static int SYSERR = 6;
final static String SYSERROUT="ERROUT";
final static int CUSTOM = 7;
final static String CUSTOMOUT="CUSTOM";
final static String ALLOWNL="ALLOWNL";
final static String FIRSTOFMULTILINEREGEX="FIRSTOFMULTILINEREGEX";
def customerOutputter = null;
final static String DEFAULTLOC= "DEFAULT-LOCATION";
final static String DEFAULTPROC= "DEFAULT-PROCESS";
final static String ISVERBOSE= "VERBOSE";
final static String TIME = "%t";
final static String LOGLEVEL = "%l";
final static String LOCATION = "%c";
final static String MESSAGE = "%m";
final static String PROCESS = "%p";
final static String LOOPCOUNTER = "%i";
final static String ITERCOUNTER = "%j";
final static String PROPFILENAMEDEFAULT = "tool.properties";
static int defaultLogDelay = 0;
// cheat shouldn't be static
//TODO
private boolean verbose = false; // allows us to pretty print all the API calls if necessary
private static boolean debug = true; // these log messages are for debugging only
private Logger juLogger = null;
final static HashMap<String, Level> JULMAPPER = new HashMap<String, Level>() {{ put("WARNING", Level.WARNING);
put("WARN", Level.SEVERE);
put("SEVERE", Level.SEVERE);
put("ERROR", Level.SEVERE);
put("FATAL", Level.SEVERE);
put("INFO", Level.INFO);
put("INFORMATION", Level.INFO);
put("CONFIG", Level.CONFIG);
put("FINE", Level.FINE);
put("FINER", Level.FINER);
put("FINEST", Level.FINEST);
put("TRACE", Level.FINE);
}};
/*
* This defines the interface to be used by any custom log output implementations
*/
public interface RecordLogEvent
{
/*
* Sets the outputter up with the loadded properties files etc. The implementation needs
* to build all the necessary resources ready so that the execution of the outputs can run
* to the simulated timing
*/
public void initialize (Properties props);
public void writeLogEntry(String entry);
public void clearDown();
}
/*
* This defines the interface that the custom handlers need to implement.
*/
class LogToConsole implements RecordLogEvent
{
public void initialize (Properties props) { }
public void writeLogEntry(String entry)
{
System.out.println (entry);
}
public void clearDown() {
// nothing to do here
}
}
// static variable necessary so we can use the object in our unit tests - working around visibility
// constraint
static public LogEntry testLogEntry = new LogEntry();
/**
* This class holds the parsed log entry to be used
*/
static class LogEntry
{
public static defaultLogLevel = "";
public static defaultProcess = "";
public static defaultLocation = "";
public int offset = 0; // time in millis
public String logLevel = defaultLogLevel; // string representing the log level
public String process = defaultProcess; // presents the process name or thread
public String location = defaultLocation; // class path etc
public String message = ""; // core message
/**
* Used for inspacting the log values held
*/
public String toString ()
{
return "offset="+offset+"|"+logLevel+"|"+process+"|"+location+"|"+message;
}
}
/*
*Translates the configuration file to the Java Util level object.
*/
static Level toJULLevel (String level, Properties props)
{
if (level == null)
{
level = props.get(DEFAULTLOGLEVEL)
}
try
{
return JULMAPPER.getOrDefault(level.toUpperCase(), Level.INFO);
}
catch (Exception err)
{
System.out.println ("caught error looking up JUL code");
return Level.INFO;
}
}
static int getOutputType (Properties props, boolean verbose)
{
int outType = UNKNOWNOUTPUT;
String propOut = props.get(OUTTYPE);
if ((propOut != null) && (propOut.length() > 0))
{
if (propOut.equalsIgnoreCase(FILE))
{
outType = FILEOUTPUT;
}
else if (propOut.equalsIgnoreCase(CONSOLE))
{
outType = CONSOLEOUTPUT;
}
else if (propOut.equalsIgnoreCase(HTTP))
{
outType = HTTPOUTPUT;
}
else if (propOut.equalsIgnoreCase(TCPOUT))
{
outType = TCPOUTPUT;
}
else if (propOut.equalsIgnoreCase(JULOUT))
{
outType = JUL;
}
else if (propOut.equalsIgnoreCase(SYSSTDOUT))
{
outType = SYSSTD;
}
else if (propOut.equalsIgnoreCase(SYSERROUT))
{
outType = SYSERR;
}
else if (propOut.equalsIgnoreCase(CUSTOMOUT))
{
outType = CUSTOM;
}
else
{
if (verbose){System.out.println("Unknown output type :" + props.get(OUTTYPE));}
}
}
return outType;
}
/**
* Takes the log event elements and builds the output using the formatting template
*/
static String logToString (LogEntry log, String dtgFormat, String separator, String outTemplate,
boolean verbose, int counter, int iterCount)
{
String output = null;
if (log == null)
{
if (debug) {System.out.println ("logToString - no log object" );}
return "";
}
if (outTemplate != null)
{
output = new String(outTemplate);
}
else
{
output = TIME + separator + MESSAGE;
}
if (debug) {System.out.println ("logToString iterCount=>"+iterCount + "< counter=>" + counter + "< output=>" + output + "< dtgFormat=>" + dtgFormat + "< log=>" + log +"<-" );}
if (output.indexOf (TIME) > -1)
{
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(dtgFormat);
LocalDateTime now = LocalDateTime.now();
if (debug) {System.out.println (TIME + " == " + now.format(dtf));}
output = output.replace(TIME, now.format(dtf));
}
if (output.indexOf (LOGLEVEL) > -1)
{
if (log.logLevel != null)
{
output = output.replace(LOGLEVEL, log.logLevel);
}
else
{
output = output.replace(LOGLEVEL, "");
}
}
if (output.indexOf (PROCESS) > -1)
{
if (log.process != null)
{
output = output.replace(PROCESS, log.process);
}
else
{
output = output.replace(PROCESS, "");
}
}
if (output.indexOf (LOCATION) > -1)
{
if (log.location != null)
{
output = output.replace(LOCATION, log.location);
}
else
{
output = output.replace(LOCATION, "");
}
}
if (output.indexOf (MESSAGE) > -1)
{
if (log.message != null)
{
output = output.replace(MESSAGE, log.message);
}
else
{
System.out.println ("No message to sub")
output = output.replace(MESSAGE, "");
}
}
if (output.indexOf (LOOPCOUNTER) > -1)
{
output = output.replace(LOOPCOUNTER, String.valueOf (counter));
}
if (output.indexOf (ITERCOUNTER) > -1)
{
output = output.replace(ITERCOUNTER, String.valueOf (iterCount));
}
if (debug) {System.out.println ("logToString - returns:" +output );}
return output;
}
/*
*
*/
static String displayTokens(StringTokenizer tokens)
{
String output = "";
int elementCtr = 1;
if (tokens == null)
{return "tokens is null object"}
while (tokens.hasMoreElements())
{
output = output + "elem " + elementCtr + ">" + (String)tokens.nextElement() + "<; ";
if (false) // if the token string should be multiline set to true
{output = output + "\n"}
elementCtr++;
}
return output;
}
/*
*
*/
static LogEntry createLogEntry (String line, String[] formatArray, String separator, boolean verbose)
{
LogEntry aLogEntry = new LogEntry();
if ((line == null) || (line.length() == 0))
{
if (verbose) {System.out.println ("createLogEntry - empty line");}
return null;
}
StringTokenizer st = new StringTokenizer(line, separator); // why does it fail when we pass in the separator
int fmtIdx = 0;
String element = null;
boolean valueSet = false;
//if (debug)
//{
// System.out.println ("createLogEntry token count>" + st.countTokens());
// System.out.println ("createLogEntry Line>" + line + "<\n"+displayTokens(st)+"<--");
//}
aLogEntry.offset = defaultLogDelay;
while (st.hasMoreElements())
{
element = (String)st.nextElement();
if (debug) {System.out.println ("createLogEntry Line>" + fmtIdx + "< >" + formatArray.length+"<" + " " + valueSet);}
while ((fmtIdx < formatArray.length) && (!valueSet))
{
element = element.trim();
if (formatArray[fmtIdx].equals (TIME))
{
if (element.charAt(0) == '+')
{
aLogEntry.offset = Integer.parseInt(element.substring(1));
valueSet = true;
}
else
{
// to convert date time to offset
if (verbose) {System.out.println ("createLogEntry - need to convert to offset");}
}
}
else if (formatArray[fmtIdx].equals (LOGLEVEL))
{
aLogEntry.logLevel = element;
valueSet = true;
}
else if (formatArray[fmtIdx].equals (LOCATION))
{
aLogEntry.location = element;
valueSet = true;
}
else if (formatArray[fmtIdx].equals (MESSAGE))
{
aLogEntry.message = element;
while (st.hasMoreElements())
{
aLogEntry.message = aLogEntry.message + separator + st.nextElement();
}
valueSet = true;
}
else if (formatArray[fmtIdx].equals (PROCESS))
{
aLogEntry.process = element;
valueSet = true;
}
else
{
if (verbose) {System.out.println ("Unrecognized formatter code : " + formatArray[fmtIdx]);}
}
fmtIdx++;
}
valueSet = false;
}
if (verbose){System.out.println ("entry ==>" + aLogEntry.toString());}
return aLogEntry;
}
static String mergeToString (ArrayList<String> staging, boolean verbose = false, boolean allowNL = false)
{
Iterator iter = staging.iterator();
String mergeStr = null;
while (iter.hasNext())
{
if (mergeStr == null)
{
if (debug){System.out.println ("mergeToString start string");};
mergeStr = iter.next();
}
else
{
if (debug){System.out.println ("mergeToString EXTEND string");};
mergeStr = mergeStr + "\n" + iter.next();
}
}
if (allowNL)
{
mergeStr = mergeStr.replace ('\\n', '\n');
}
if (debug){System.out.println ("mergeToString result >>>>"+mergeStr+"<<<<");}
return mergeStr;
}
static ArrayList<LogEntry> simpleRead (BufferedReader sourceReader, String separator, String[] formatArray, boolean verbose, boolean allowNL)
{
ArrayList<LogEntry> lines = new ArrayList<LogEntry> ();
if (verbose){System.out.println ("simpleRead>"+separator+"<\n" + formatArray+ "\n" + allowNL);}
String line = sourceReader.readLine();
while (line != null)
{
if (allowNL)
{
line = line.replace ('\\n', '\n');
}
LogEntry log = createLogEntry (line, formatArray, separator, verbose);
if (log != null)
{
lines.add(log);
}
else
{
if (verbose){System.out.println ("simpleRead rec'd null line entry - ignoring");}
}
line = sourceReader.readLine();
}
return lines;
}
/*
* This works by reading a line at a time. If the line doesnt match the regex
* it is corporated into the current record with a newline character
*/
static ArrayList<LogEntry> multiLineRead (BufferedReader sourceReader,
String separator,
String[] formatArray,
boolean verbose,
String regex,
boolean allowNL)
{
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher matcher = null;
ArrayList<String> lines = new ArrayList<String> ();
ArrayList<String> staging = new ArrayList<String> ();
String line = sourceReader.readLine();
Boolean foundNewLogLine = false;
while (line != null)
{
matcher = pattern.matcher(line);
foundNewLogLine =matcher.find();
if (verbose){System.out.println ("foundNewLogLine="+foundNewLogLine);}
if (foundNewLogLine)
{
if (debug){println ("new line identified, staging is " + staging.size());}
if (!staging.isEmpty())
{
LogEntry log = createLogEntry (mergeToString (staging, verbose, allowNL), formatArray, separator, verbose);
if (log != null)
{
lines.add (log);
if (verbose){System.out.println ("multiLineRead - added log");}
}
else
{
if (verbose){System.out.println ("multiLineRead - rec'd a null log entry ignoring");}
}
staging.clear();
}
staging.add(line);
if (debug){System.out.println ("adding (1)>"+line+"< to staging");}
}
else
{
staging.add(line);
if (debug){System.out.println ("adding (2)>"+line+"< to staging")};
}
line = sourceReader.readLine();
}
if (!staging.isEmpty())
{
if (verbose){System.out.println ("final merge")};
String merged = mergeToString (staging, verbose, allowNL);
lines.add (createLogEntry (merged, formatArray, separator, verbose));
}
return lines;
}
/*
* The process of reading the log file is performed. With the config for the file structure
* parsed into an arraylist so that it directs the logb input processor.
* an ArrayList of objects representing each record to be played out
*/
static ArrayList<LogEntry> loadLogs (String source,
String separator,
String format,
boolean verbose,
String multiLineREGEX,
boolean allowNL)
{
BufferedReader sourceReader = new BufferedReader(new FileReader(source)); //creates a buffering character input stream
ArrayList<LogEntry> lines = null;
String[] formatArray = format.split (" ");
for (int idx = 0; idx < formatArray.length; idx++)
{
formatArray[idx] = formatArray[idx].trim();
if (verbose) { System.out.println ("load logs format>" + idx + "=" + formatArray[idx]); }
}
if (verbose){System.out.println ("multiline="+multiLineREGEX)}
if (multiLineREGEX == null)
{
lines = simpleRead (sourceReader, separator, formatArray, verbose, allowNL);
}
else
{
lines = multiLineRead (sourceReader, separator, formatArray, verbose, multiLineREGEX, allowNL);
}
return lines;
}
public boolean getPropAsBoolean (Properties props, String propName)
{
boolean property = false;
if (props == null)
{
return false;
}
if ((props.get(propName) != null) && (props.get(propName).equalsIgnoreCase("true")))
{
property=true;
}
else
{
property = false;
}
return property;
}
public void core (String[] args)
{
System.out.println ("Starting ...");
HashMap<Integer, RecordLogEvent> eventRecorders = new HashMap<Integer, RecordLogEvent>();
//eventRecorders.put(CONSOLEOUTPUT, new LogToConsole());
String propFilename = PROPFILENAMEDEFAULT;
String sourceFilename = null;
Properties props = new Properties();
// process the command line properties
if (args.size() > 0)
{
if (args[0].equalsIgnoreCase("-h"))
{
System.out.println(HELPMSG);
System.exit(-1);
}
else
{
propFilename = args[0];
println ("Going to use " + propFilename);
if (args.size() > 1)
{
// we've been given the data file in the command line - this trumps any file set in the properties
sourceFilename = args[1];
if (sourceFilename != null)
{
sourceFilename = sourceFilename.trim();
if (sourceFilename.length() < 1)
{
sourceFilename = null;
}
}
}
}
}
else
{
println("going to use default properties file");
}
try
{
println ("handling properties file - " + propFilename);
File propFile = new File(propFilename);
props.load(propFile.newDataInputStream());
String sourceSeparator = props.get (SOURCESEPARATOR);
String targetSeparator = props.get (TARGETSEPARATOR);
if ((sourceSeparator == null) || (sourceSeparator.size() == 0))
{
props.put (SOURCESEPARATOR, " ");
sourceSeparator = props.get (SOURCESEPARATOR);
}
if ((targetSeparator == null) || (targetSeparator.size() == 0))
{
props.put (TARGETSEPARATOR, " ");
targetSeparator = props.get (TARGETSEPARATOR);
}
if (sourceFilename != null)
{
props.put (SOURCEFILE, sourceFilename);
}
}
catch (Exception err)
{
println("Couldn't manage properties:\n" + err.getMessage());
println(err.getStackTrace());
println(HELPMSG);
System.exit(-1);
}
// verify all the parameters
try
{
assert ((props.get(SOURCEFILE) != null) && (props.get(SOURCEFILE).size() > 0)): "TARGET not defined";
assert ((props.get(SOURCEFORMAT) != null) && (props.get(SOURCEFORMAT).size() > 0)): "No formatting for output defined";
}
catch (AssertionError err)
{
System.out.println(err.getMessage());
System.out.println(HELPMSG);
System.exit(-1);
}
if ((props.get(ISVERBOSE) != null) && (props.get(ISVERBOSE).equalsIgnoreCase("true")))
{
verbose=true;
System.out.println ("In verbose mode");
}
else
{
verbose = false;
}
if ((props.get(DEFAULTLOC) != null) && (props.get(DEFAULTLOC).length() > 0))
{
LogEntry.defaultLocation = props.get(DEFAULTLOC);
}
if ((props.get(DEFAULTPROC) != null) && (props.get(DEFAULTPROC).length() > 0))
{
LogEntry.defaultProcess= props.get(DEFAULTPROC);
}
int accelerationFactor =1;
if ((props.get(ACCELERATOR) != null) && (props.get(ACCELERATOR).length() > 0))
{
try
{
accelerationFactor = Integer.parseInt(props.get(ACCELERATOR));
if (verbose) {System.out.println ("Replay acceleration by " + accelerationFactor);}
}
catch (NumberFormatException err)
{
System.out.println ("Couldn't process accelerator value >" + props.get(ACCELERATOR)+"<");
}
}
if ((props.get(DEFAULTDELAYOFFSET) != null) && (props.get(DEFAULTDELAYOFFSET).length() > 0))
{
try
{
defaultLogDelay = Integer.parseInt(props.get(DEFAULTDELAYOFFSET));
if (verbose) {System.out.println ("Set default delay to " + defaultLogDelay);}
}
catch (NumberFormatException err)
{
System.out.println ("Couldn't process default delay value >" + props.get(DEFAULTDELAYOFFSET)+"<");
}
}
int outputType = getOutputType(props, verbose);
//see if the custom outputtype is set
if (outputType == CUSTOM)
{
String outputImpPath = null;
try
{
if (verbose) {System.out.println ("customer outputter requested");}
outputImpPath = props.get(CUSTOMOUTTYPE).trim();
customerOutputter = null;
if (verbose) {System.out.println ("customer outputter loading - " + outputImpPath);}
Class instanceClass = Class.forName(outputImpPath);
if (verbose) {System.out.println ("class outputter is - " + instanceClass.getName());}
RecordLogEvent outputterInstance = instanceClass.newInstance();
outputterInstance.initialize(props);
eventRecorders.put(CUSTOM, outputterInstance);
}
catch (Exception err)
{
System.out.println ("Error trying to prepare custom output type: "+outputImpPath+"\n" + err.toString());
err.printStackTrace();
System.exit(-1);
}
}
// initialize the default values
LogEntry.defaultLocation = props.get(DEFAULTLOC, "");
LogEntry.defaultLogLevel = props.get(DEFAULTLOGLEVEL, "");
LogEntry.defaultProcess = props.get(DEFAULTPROC, "");
ArrayList<LogEntry> logs = loadLogs (props.get(SOURCEFILE),
props.get (SOURCESEPARATOR),
props.get (SOURCEFORMAT),
verbose,
props.get(FIRSTOFMULTILINEREGEX),
getPropAsBoolean(props, ALLOWNL));
if (verbose) {System.out.println ("Logs now loaded");}
LogEntry log = null;
String dtgFormat = "HH:mm:ss";
if ((props.get(TARGETDTG) != null) && (props.get(TARGETDTG).length() > 0))
{
dtgFormat = props.get(TARGETDTG);
if (verbose) {System.out.println ("Date time format " + dtgFormat);}
}
int loopTotal = 1;
int loopCount = 0;
if (props.get(LOOP) != null)
{
try
{
loopTotal = Integer.parseInt(props.get(LOOP));
if (verbose) {System.out.println ("Number of loops set is " + loopTotal);}
}
catch (NumberFormatException err)
{
System.out.println ("Couldn't process loop counter >" + props.get(LOOP)+"<");
}
}
try
{
while (loopCount < loopTotal)
{
int lineCount = 0;
loopCount++;
if (verbose) {System.out.println ("Performing data set pass " + loopCount + " of " + loopTotal);}
Iterator iter = logs.iterator();
String separator = props.get (TARGETSEPARATOR);
BufferedWriter bufferedWriter = null;
while (iter.hasNext())
{
lineCount++;
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(dtgFormat);
LocalDateTime now = LocalDateTime.now();
log = (LogEntry) iter.next();
String output = logToString(log, dtgFormat, separator, props.get(TARGETFORMAT), verbose, loopCount, lineCount);
String iterCount = "";
switch(outputType)
{
case CONSOLEOUTPUT:
System.out.println ("Console:" + output);
break
case FILEOUTPUT:
if (bufferedWriter == null)
{
assert ((props.get(TARGETFILE) != null) && (props.get(TARGETFILE).size() > 0)): "No target file for output defined";
bufferedWriter = new BufferedWriter(new FileWriter(props.get(TARGETFILE), true));
}
bufferedWriter.write(output+"\n");
bufferedWriter.flush();
break;
case HTTPOUTPUT:
if (verbose) {System.out.println ("HTTP:" + output);}
URL baseUrl = new URL(props.get(TARGETURL));
URLConnection webConnection = baseUrl.openConnection();
webConnection.doOutput = true;
webConnection.requestMethod = 'POST';
webConnection.setRequestProperty("content-type", "application/json");
boolean sent = false;
boolean errCaught = false;
while (!sent)
{
try{
webConnection.with {
outputStream.withWriter { writer -> writer << output }
outputStream.flush();
}
String response= webConnection.getContent();
sent = true;
if (errCaught && verbose)
{
System.out.println ("Connection resolved, event sent");
}
}
catch (Exception err)
{
if (verbose) {System.out.println ("Err - try again in a a moment \n"+err.toString())}
sleep (100);
errCaught = true;
}
}
break;
case TCPOUTPUT:
if (verbose) {System.out.println ("about to fire TCP:" + output);}
Socket sock = new Socket(props.get(TARGETIP), Integer.parseInt(props.get(TARGETPORT)));
OutputStream outStream = sock.getOutputStream();
Writer writer = new PrintWriter (outStream, true);
writer.println (output);
writer.close();
sock.close();
break;
case JUL:
if (juLogger == null)
{
LogManager manager = LogManager.getLogManager();
String loggerConfig = props.get(JULCONFIG);
if (loggerConfig != null)
{
System.out.println ("properties:" + loggerConfig);
manager.readConfiguration(new FileInputStream(loggerConfig));
}
String loggerName = props.get(JULNAME);
if (loggerName == null)
{
loggerName = "";
}
juLogger = Logger.getLogger (loggerName);
if (verbose) {System.out.println ("Created JUL Logger called " + juLogger.getName());}
}
if (verbose) {System.out.println ("about to fire Java Util Logging ("+toJULLevel(log.logLevel, props)+") " + log.message);}
try
{
LogRecord record = new LogRecord (toJULLevel(log.logLevel, props), output);
record.setSourceClassName (log.location);
record.setLoggerName("LogSimulator");
juLogger.log (record);
}
catch (Exception err)
{
if (verbose) {System.out.println ("Failed to log " + log.toString());}
}
break;
case SYSSTD:
System.out.println (output);
break;
case SYSERR:
System.err.println (output);
break;
case CUSTOM:
eventRecorders.get(outputType).writeLogEntry(output);
break
default:
if (verbose) {System.out.println ("defaulted==>" + getOutputType(props, verbose));}
}
if (log != null)
{
try
{
sleep (Math.round((log.offset)/accelerationFactor));
}
catch (Exception err)
{
sleep (log.offset);
}
}
}