| 57 | | ) |
| 58 | | dispatch := method(handler, |
| 59 | | listeners foreach(listener, |
| 60 | | e := try( |
| 61 | | listener call(handler) |
| 62 | | ) |
| 63 | | e catch(Exception, |
| 64 | | Exception raise("could not run the listener: " .. listener .. "\n" .. e) |
| 65 | | ) |
| 66 | | ) |
| 67 | | ) |
| 68 | | findListenerIndexes := method(listener, |
| 69 | | indexes := List clone |
| 70 | | listeners foreach(index, value, |
| 71 | | if(value == listener) then( |
| 72 | | indexes append(index) |
| 73 | | ) |
| 74 | | ) |
| 75 | | indexes |
| 76 | | ) |
| 77 | | ) |
| 78 | | |
| 79 | | Log4Io Level := Object clone do( |
| 80 | | level ::= nil |
| 81 | | name ::= nil |
| 82 | | with := method(level, name, |
| 83 | | self clone setLevel(level) setName(name) |
| 84 | | ) |
| 85 | | toLevel := method(level, defaultLevel, |
| 86 | | if(level isNil) then( |
| 87 | | return defaultLevel |
| 88 | | ) |
| 89 | | if(level isKindOf(Sequence)) then( |
| 90 | | return level asUppercase switch( |
| 91 | | ALL name, ALL, |
| 92 | | DEBUG name, DEBUG, |
| 93 | | INFO name, INFO, |
| 94 | | WARN name, WARN, |
| 95 | | ERROR name, ERROR, |
| 96 | | FATAL name, FATAL, |
| 97 | | OFF name, OFF, |
| 98 | | TRACE name, TRACE, |
| 99 | | defaultLevel |
| 100 | | ) |
| 101 | | ) |
| 102 | | if(level isKindOf(Number)) then( |
| 103 | | return level switch( |
| 104 | | ALL_INT, ALL, |
| 105 | | DEBUG_INT, DEBUG, |
| 106 | | INFO_INT, INFO, |
| 107 | | WARN_INT, WARN, |
| 108 | | ERROR_INT, ERROR, |
| 109 | | FATAL_INT, FATAL, |
| 110 | | OFF_INT, OFF, |
| 111 | | TRACE_INT, TRACE, |
| 112 | | defaultLevel |
| 113 | | ) |
| 114 | | ) |
| 115 | | defaultLevel |
| 116 | | ) |
| 117 | | asString := method(name) |
| 118 | | valueOf := method(level) |
| 119 | | ) |
| 120 | | |
| 121 | | Log4Io Level do( |
| 122 | | OFF_INT := Number integerMax |
| 123 | | FATAL_INT := 50000 |
| 124 | | ERROR_INT := 40000 |
| 125 | | WARN_INT := 30000 |
| 126 | | INFO_INT := 20000 |
| 127 | | DEBUG_INT := 10000 |
| 128 | | TRACE_INT := 5000 |
| 129 | | ALL_INT := Number integerMin |
| 130 | | ) |
| 131 | | |
| 132 | | Log4Io Level do( |
| 133 | | OFF := Log4Io Level with(OFF_INT, "OFF") |
| 134 | | FATAL := Log4Io Level with(FATAL_INT, "FATAL") |
| 135 | | ERROR := Log4Io Level with(ERROR_INT, "ERROR") |
| 136 | | WARN := Log4Io Level with(WARN_INT, "WARN") |
| 137 | | INFO := Log4Io Level with(INFO_INT, "INFO") |
| 138 | | DEBUG := Log4Io Level with(DEBUG_INT, "DEBUG") |
| 139 | | TRACE := Log4Io Level with(TRACE_INT, "TRACE") |
| 140 | | ALL := Log4Io Level with(ALL_INT, "ALL") |
| 141 | | ) |
| 142 | | |
| 143 | | Log4Io Logger := Object clone do( |
| 144 | | level ::= nil |
| 145 | | dateFormat ::= nil |
| 146 | | with := method(name, |
| 147 | | c := self clone |
| 148 | | c category := if(name isNil, "", name) |
| 149 | | c dateformat := Log4Io DateFormatter DEFAULT_DATE_FORMAT |
| 150 | | c dateformatter := Log4Io DateFormatter clone |
| 151 | | c onlog := Log4Io CustomEvent clone |
| 152 | | c onclear := Log4Io CustomEvent clone |
| 153 | | c loggingEvents := List clone |
| 154 | | c appenders := List clone |
| 155 | | c level := nil |
| 156 | | c appenders append(Log4Io Appender with(c)) |
| 157 | | c |
| 158 | | ) |
| 159 | | addAppender := method(appender, |
| 160 | | if(appender isKindOf(Log4Io Appender) not) then( |
| 161 | | Exception raise("Not kindOf an Appender: " .. appender) |
| 162 | | ) |
| 163 | | appender setLogger(self) |
| 164 | | appenders append(appender) |
| 165 | | ) |
| 166 | | setAppenders := method(appenders, |
| 167 | | self appenders foreach(appender, appender doClear) |
| 168 | | self appenders = appenders |
| 169 | | self appenders foreach(appender, |
| 170 | | appender setLogger(self) |
| 171 | | ) |
| 172 | | ) |
| 173 | | getFormattedTimestamp := method(date, |
| 174 | | self dateformatter format(date, dateformat) |
| 175 | | ) |
| 176 | | ) |
| 177 | | |
| 178 | | Log4Io Logger do( |
| 179 | | log := method(level, message, exception, |
| 180 | | event := Log4Io LoggingEvent with(category, level, message, exception, self) |
| 181 | | loggingEvents append(event) |
| 182 | | onlog dispatch(event) |
| 183 | | ) |
| 184 | | clear := method( |
| 185 | | e := try( |
| 186 | | loggingEvents = List clone |
| 187 | | onclear dispatch |
| 188 | | ) |
| 189 | | e pass |
| 190 | | ) |
| 191 | | isTraceEnabled := method( |
| 192 | | level valueOf <= Log4Io Level TRACE valueOf |
| 193 | | ) |
| 194 | | trace := method(message, exception, |
| 195 | | log(Log4Io Level TRACE, message, exception) |
| 196 | | ) |
| 197 | | isDebugEnabled := method( |
| 198 | | level valueOf <= Log4Io Level DEBUG valueOf |
| 199 | | ) |
| 200 | | debug := method(message, exception, |
| 201 | | log(Log4Io Level DEBUG, message, exception) |
| 202 | | ) |
| 203 | | isInfoEnabled := method( |
| 204 | | level valueOf <= Log4Io Level INFO valueOf |
| 205 | | ) |
| 206 | | info := method(message, exception, |
| 207 | | log(Log4Io Level INFO, message, exception) |
| 208 | | ) |
| 209 | | isWarnEnabled := method( |
| 210 | | level valueOf <= Log4Io Level WARN valueOf |
| 211 | | ) |
| 212 | | warn := method(message, exception, |
| 213 | | log(Log4Io Level WARN, message, exception) |
| 214 | | ) |
| 215 | | ifErrorEnabled := method( |
| 216 | | level valueOf <= Log4Io Level ERROR valueOf |
| 217 | | ) |
| 218 | | error := method(message, exception, |
| 219 | | log(Log4Io Level ERROR, message, exception) |
| 220 | | ) |
| 221 | | isFatalEnabled := method( |
| 222 | | level valueOf <= Log4Io Level FATAL valueOf |
| 223 | | ) |
| 224 | | fatal := method(message, exception, |
| 225 | | log(Log4Io Level FATAL, message, exception) |
| 226 | | ) |
| 227 | | ) |
| 228 | | |
| 229 | | Log4Io LoggingEvent := Object clone do( |
| 230 | | startTime := nil |
| 231 | | categoryName := nil |
| 232 | | message := nil |
| 233 | | exception := nil |
| 234 | | level := nil |
| 235 | | logger := nil |
| 236 | | |
| 237 | | with := method(categoryName, level, message, exception, logger, |
| 238 | | c := self clone |
| 239 | | c startTime := Date clone |
| 240 | | c categoryName = categoryName |
| 241 | | c level = level |
| 242 | | c message = message |
| 243 | | c exception = exception |
| 244 | | c logger = logger |
| 245 | | c |
| 246 | | ) |
| 247 | | getFormattedTimestamp := method( |
| 248 | | if(logger isNil not) then( |
| 249 | | return logger getFormattedTimestamp(startTime) |
| 250 | | ) |
| 251 | | return startTime asAtomDate |
| 252 | | ) |
| 253 | | ) |
| 254 | | |
| 255 | | Log4Io Layout := Object clone do ( |
| 256 | | separator ::= "" |
| 257 | | init := method(nil) |
| 258 | | with := method() |
| 259 | | format := method(event, |
| 260 | | "" |
| 261 | | ) |
| 262 | | getSeparator := method(separator) |
| 263 | | ) |
| 264 | | |
| 265 | | Log4Io SimpleLayout := Log4Io Layout clone do ( |
| 266 | | with = method( |
| 267 | | setSeparator("\n") |
| 268 | | ) |
| 269 | | |
| 270 | | format = method(event, |
| 271 | | event level asString .. " - " .. event message .. separator |
| 272 | | ) |
| 273 | | ) |
| 274 | | |
| 275 | | Log4Io BasicLayout := Log4Io Layout clone do( |
| 276 | | with = method( |
| 277 | | setSeparator("\n") |
| 278 | | ) |
| 279 | | |
| 280 | | format = method(event, |
| 281 | | event categoryName .. "~" .. event startTime asString .. "[" .. event level asString .. "]" .. event message .. separator |
| 282 | | ) |
| 283 | | ) |
| 284 | | |
| 285 | | Log4Io PatternLayout := Log4Io Layout clone do( |
| 286 | | pattern ::= nil |
| 287 | | Formatter := Object clone do( |
| 288 | | loggingEvent ::= nil |
| 289 | | format := method(source, |
| 290 | | buf := Sequence clone asBuffer |
| 291 | | regex := Regex with("%(-?[0-9]+)?(\.?[0-9]+)?([cdmnpr%])(\{([^\}]+)\})?|([^%]+)") |
| 292 | | regex matchesIn(source) map(match, |
| 293 | | holder := match string |
| 294 | | value := if(hasSlot(holder), perform(holder), holder) |
| 295 | | buf appendSeq(value) |
| 296 | | ) |
| 297 | | buf asString |
| 298 | | ) |
| 299 | | setSlot("%c", method( |
| 300 | | loggingEvent categoryName |
| 301 | | )) |
| 302 | | setSlot("%d", method( |
| 303 | | format := Log4Io SimpleDateFormat with(Log4Io PatternLayout ISO8601_DATEFORMAT) |
| 304 | | format format(loggingEvent startTime) |
| 305 | | )) |
| 306 | | setSlot("%m", method( |
| 307 | | loggingEvent message |
| 308 | | )) |
| 309 | | setSlot("%n", method( |
| 310 | | "\n" |
| 311 | | )) |
| 312 | | setSlot("%p", method( |
| 313 | | loggingEvent level asString |
| 314 | | )) |
| 315 | | setSlot("%r", method( |
| 316 | | loggingEvent startTime asString |
| 317 | | )) |
| 318 | | setSlot("%%", method( |
| 319 | | "%" |
| 320 | | )) |
| 321 | | ) |
| 322 | | with := method(pattern, |
| 323 | | c := self clone |
| 324 | | if(pattern isNil not) then ( |
| 325 | | c pattern = pattern |
| 326 | | ) else ( |
| 327 | | c pattern = Log4Io PatternLayout DEFAULT_CONVERSION_PATTERN |
| 328 | | ) |
| 329 | | c |
| 330 | | ) |
| 331 | | format := method(event, |
| 332 | | formatter := Formatter clone |
| 333 | | formatter setLoggingEvent(event) |
| 334 | | formatter format(pattern) |
| 335 | | ) |
| 336 | | ) |
| 337 | | |
| 338 | | Log4Io PatternLayout do ( |
| 339 | | TTCC_CONVERSION_PATTERN := "%r %p %c - %m%n" |
| 340 | | DEFAULT_CONVERSION_PATTERN := "%m%n" |
| 341 | | |
| 342 | | ISO8601_DATEFORMAT := "yyyy-MM-dd HH:mm:ss,SSS" |
| 343 | | DATETIME_DATEFORMAT := "dd MMM YYY HH:mm:ss,SSS" |
| 344 | | ABSOLUTETIME_DATEFORMAT := "HH:mm:ss,SSS" |
| 345 | | ) |
| 346 | | Log4Io Appender := Object clone do( |
| 347 | | logger ::= nil |
| 348 | | layout ::= nil |
| 349 | | |
| 350 | | with := method() |
| 351 | | |
| 352 | | doAppend := block(event, nil) |
| 353 | | doClear := block() |
| 354 | | |
| 355 | | setLogger := method(logger, |
| 356 | | logger onlog addListener(self doAppend bind(self)) |
| 357 | | logger onclear addListener(self doClear bind(self)) |
| 358 | | |
| 359 | | self logger = logger |
| 360 | | ) |
| 361 | | ) |
| 362 | | |
| 363 | | Log4Io ConsoleAppender := Log4Io Appender clone do( |
| 364 | | with = method( |
| 365 | | c := self clone |
| 366 | | c layout = Log4Io PatternLayout with(Log4Io PatternLayout TTCC_CONVERSION_PATTERN) |
| 367 | | c |
| 368 | | ) |
| 369 | | |
| 370 | | doAppend := block(event, |
| 371 | | write(layout format(event)) |
| 372 | | ) |
| 373 | | |
| 374 | | asString := method( |
| 375 | | "Log4Io ConsoleAppender" |
| 376 | | ) |
| 377 | | ) |
| 378 | | |
| 379 | | Log4Io FileAppender := Log4Io Appender clone do( |
| 380 | | file ::= nil |
| 381 | | with = method(path, |
| 382 | | filePath = if(path isNil, "log4io.log", path) |
| 383 | | c := self clone |
| 384 | | c file := File with(filePath) |
| 385 | | c layout = Log4Io SimpleLayout with |
| 386 | | c |
| 387 | | ) |
| 388 | | |
| 389 | | doAppend := block(event, |
| 390 | | e := try( |
| 391 | | if(file isOpen not) then ( |
| 392 | | file openForAppending |
| 393 | | ) |
| 394 | | file write(layout format(event)) |
| 395 | | file close |
| 396 | | ) |
| 397 | | e catch(Exception, |
| 398 | | log4ioLogger error(e) |
| 399 | | ) |
| 400 | | ) |
| 401 | | |
| 402 | | doClear := block( |
| 403 | | e := try( |
| 404 | | if(file isOpen not) then( |
| 405 | | file open |
| 406 | | ) |
| 407 | | file setContents("") |
| 408 | | file close |
| 409 | | ) |
| 410 | | e catch(Exception, |
| 411 | | log4ioLogger error(e) |
| 412 | | ) |
| 413 | | ) |
| 414 | | |
| 415 | | asString := method( |
| 416 | | "Log4Io FileAppender[" .. file contents .. "]" |
| 417 | | ) |
| 418 | | ) |
| 419 | | Log4Io DateFormatter := Object clone do( |
| 420 | | format := method(date, dateFormat |
| 421 | | rep := Map clone |
| 422 | | rep atPut("yyyy", "%Y") |
| 423 | | rep atPut("MM", "%m") |
| 424 | | rep atPut("dd", "%d") |
| 425 | | rep atPut("hh", "%H") |
| 426 | | rep atPut("mm", "%M") |
| 427 | | rep atPut("ss", "%S") |
| 428 | | d := date copy |
| 429 | | d format := dateFormat replaceMap(rep) |
| 430 | | d asString |
| 431 | | ) |
| 432 | | ) |
| 433 | | |
| 434 | | Log4Io DateFormatter do ( |
| 435 | | DEFAULT_DATE_FORMAT := "yyyy-MM-ddThh:mm:ss0" |
| 436 | | ) |
| 437 | | |
| 438 | | Log4Io SimpleDateFormat := Log4Io DateFormatter clone do( |
| 439 | | pattern ::= nil |
| 440 | | with := method(pattern, |
| 441 | | self pattern if(pattern isNil, Log4Io DateFormatter DEFAULT_DATE_FORMAT, pattern) |
| 442 | | ) |
| 443 | | format := method(date, |
| 444 | | DateFormatter format(date, pattern) |