001/******************************************************************************* 002The MIT License (MIT) 003 004Copyright (c) 2024 KILLCODING.COM 005 006Permission is hereby granted, free of charge, to any person obtaining a copy 007of this software and associated documentation files (the "Software"), to deal 008in the Software without restriction, including without limitation the rights 009to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 010copies of the Software, and to permit persons to whom the Software is 011furnished to do so, subject to the following conditions: 012 013The above copyright notice and this permission notice shall be included in 014all copies or substantial portions of the Software. 015 016THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 017IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 018FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 019AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 020LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 021OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 022THE SOFTWARE. 023*****************************************************************************/ 024package com.killcoding.log; 025 026import java.text.SimpleDateFormat; 027import java.util.Date; 028import java.util.Map; 029import java.util.HashMap; 030import java.io.StringWriter; 031import java.io.PrintWriter; 032import java.util.List; 033import java.util.Arrays; 034import com.killcoding.tool.CommonTools; 035import com.killcoding.tool.ConfigProperties; 036import com.killcoding.tool.CodeEscape; 037import com.killcoding.log.Logger; 038 039/** 040 * This is logger class 041 * Sample, 042 * Logger log = LoggerFactory.getLogger(clazz); 043 * */ 044public final class Logger { 045 046 private final static String DEFAULT_MESSAGE_FORMAT = ":level :log_time_format [:log_thread_name-:log_thread_id] :log_class_name.:log_method_name(:log_file_name::log_line_number) **:message** \r\n:stack_trace"; 047 048 public final static List<String> LEVEL_LIST = Arrays.asList(new String[] { "DEBUG", "INFO", "WARN", "ERROR" }); 049 public final static Integer LEVEL_DEBUG = 0; 050 public final static Integer LEVEL_INFO = 1; 051 public final static Integer LEVEL_WARN = 2; 052 public final static Integer LEVEL_ERROR = 3; 053 public final static Integer LEVEL_MARK = 4; 054 055 private static Integer LEVEL = LEVEL_DEBUG; 056 057 private String clazzName; 058 059 private String hostname; 060 061 private static String classNameEnableRegex = null; 062 private static String classNameDisableRegex = null; 063 064 public Logger(Class clazz) { 065 super(); 066 this.clazzName = clazz.getName(); 067 hostname = CommonTools.getHostname(); 068 } 069 070 public Logger(String clazzName) { 071 super(); 072 this.clazzName = clazzName; 073 hostname = CommonTools.getHostname(); 074 } 075 076 protected static void setClassNameEnableRegex(String _classNameEnableRegex) { 077 classNameEnableRegex = _classNameEnableRegex; 078 } 079 080 protected static void setClassNameDisableRegex(String _classNameDisableRegex) { 081 classNameDisableRegex = _classNameDisableRegex; 082 } 083 084 protected static boolean checkLevel(String _level) { 085 if (_level == null) 086 return false; 087 088 return LEVEL_LIST.contains(_level); 089 } 090 091 protected static void setLevel(String _level) { 092 if (checkLevel(_level)) { 093 setLevel(LEVEL_LIST.indexOf(_level.toUpperCase())); 094 } 095 } 096 097 private static void setLevel(Integer level) { 098 LEVEL = (level == null ? LEVEL_DEBUG : level); 099 } 100 101 public boolean isDebugEnabled() { 102 return LEVEL == LEVEL_DEBUG; 103 } 104 105 public boolean isInfoEnabled() { 106 return LEVEL == LEVEL_INFO; 107 } 108 109 public boolean isWarnEnabled() { 110 return LEVEL == LEVEL_WARN; 111 } 112 113 public boolean isErrorEnabled() { 114 return LEVEL == LEVEL_ERROR; 115 } 116 117 public void debug(String message) { 118 if (LEVEL <= LEVEL_DEBUG) { 119 Map<String, Object> map = new HashMap<String, Object>(); 120 map.put("message", message); 121 log(LEVEL_DEBUG, map); 122 } 123 } 124 125 public void debug(String message, Exception exception) { 126 if (LEVEL <= LEVEL_DEBUG) { 127 Map<String, Object> map = new HashMap<String, Object>(); 128 map.put("message", message); 129 map.put("exception", exception); 130 log(LEVEL_DEBUG, map); 131 } 132 } 133 134 public void debug(Exception exception) { 135 if (LEVEL <= LEVEL_DEBUG) { 136 Map<String, Object> map = new HashMap<String, Object>(); 137 map.put("message", exception.getMessage()); 138 map.put("exception", exception); 139 log(LEVEL_DEBUG, map); 140 } 141 } 142 143 public void debug(String message, Object... params) { 144 if (LEVEL <= LEVEL_DEBUG) { 145 String msg = message.replaceAll("\\{\\}", "%s"); 146 String _message = String.format(msg, converParams(params)); 147 Map<String, Object> map = new HashMap<String, Object>(); 148 map.put("message", _message); 149 log(LEVEL_DEBUG, map); 150 } 151 } 152 153 public void info(String message) { 154 if (LEVEL <= LEVEL_INFO) { 155 Map<String, Object> map = new HashMap<String, Object>(); 156 map.put("message", message); 157 log(LEVEL_INFO, map); 158 } 159 } 160 161 public void info(String message, Exception exception) { 162 if (LEVEL <= LEVEL_INFO) { 163 Map<String, Object> map = new HashMap<String, Object>(); 164 map.put("message", message); 165 map.put("exception", exception); 166 log(LEVEL_INFO, map); 167 } 168 } 169 170 public void info(Exception exception) { 171 if (LEVEL <= LEVEL_INFO) { 172 Map<String, Object> map = new HashMap<String, Object>(); 173 map.put("message", exception.getMessage()); 174 map.put("exception", exception); 175 log(LEVEL_INFO, map); 176 } 177 } 178 179 public void info(String message, Object... params) { 180 if (LEVEL <= LEVEL_INFO) { 181 String msg = message.replaceAll("\\{\\}", "%s"); 182 String _message = String.format(msg, converParams(params)); 183 Map<String, Object> map = new HashMap<String, Object>(); 184 map.put("message", _message); 185 log(LEVEL_INFO, map); 186 } 187 } 188 189 public void warn(String message) { 190 if (LEVEL <= LEVEL_WARN) { 191 Map<String, Object> map = new HashMap<String, Object>(); 192 map.put("message", message); 193 log(LEVEL_WARN, map); 194 } 195 } 196 197 public void warn(String message, Exception exception) { 198 if (LEVEL <= LEVEL_WARN) { 199 Map<String, Object> map = new HashMap<String, Object>(); 200 map.put("message", message); 201 map.put("exception", exception); 202 log(LEVEL_WARN, map); 203 } 204 } 205 206 public void warn(Exception exception) { 207 if (LEVEL <= LEVEL_WARN) { 208 Map<String, Object> map = new HashMap<String, Object>(); 209 map.put("message", exception.getMessage()); 210 map.put("exception", exception); 211 log(LEVEL_WARN, map); 212 } 213 } 214 215 public void warn(String message, Object... params) { 216 if (LEVEL <= LEVEL_WARN) { 217 String msg = message.replaceAll("\\{\\}", "%s"); 218 String _message = String.format(msg, converParams(params)); 219 Map<String, Object> map = new HashMap<String, Object>(); 220 map.put("message", _message); 221 log(LEVEL_WARN, map); 222 } 223 } 224 225 public void error(String message) { 226 if (LEVEL <= LEVEL_ERROR) { 227 Map<String, Object> map = new HashMap<String, Object>(); 228 map.put("message", message); 229 log(LEVEL_ERROR, map); 230 } 231 } 232 233 public void error(String message, Exception exception) { 234 if (LEVEL <= LEVEL_ERROR) { 235 Map<String, Object> map = new HashMap<String, Object>(); 236 map.put("message", message); 237 map.put("exception", exception); 238 log(LEVEL_ERROR, map); 239 } 240 } 241 242 public void error(Exception exception) { 243 if (LEVEL <= LEVEL_ERROR) { 244 Map<String, Object> map = new HashMap<String, Object>(); 245 map.put("message", exception.getMessage()); 246 map.put("exception", exception); 247 log(LEVEL_ERROR, map); 248 } 249 } 250 251 public void error(String message, Object... params) { 252 if (LEVEL <= LEVEL_ERROR) { 253 String msg = message.replaceAll("\\{\\}", "%s"); 254 String _message = String.format(msg, converParams(params)); 255 Map<String, Object> map = new HashMap<String, Object>(); 256 map.put("message", _message); 257 log(LEVEL_ERROR, map); 258 } 259 } 260 261 public void mark(String message) { 262 Map<String, Object> map = new HashMap<String, Object>(); 263 map.put("message", message); 264 log(LEVEL_MARK, map); 265 } 266 267 public void mark(String message, Object... params) { 268 String msg = message.replaceAll("\\{\\}", "%s"); 269 String _message = String.format(msg, converParams(params)); 270 Map<String, Object> map = new HashMap<String, Object>(); 271 map.put("message", _message); 272 log(LEVEL_MARK, map); 273 } 274 275 public static void systemError(String clazz, String message) { 276 if (LEVEL <= LEVEL_ERROR) { 277 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 278 Map<String, Object> map = new HashMap<String, Object>(); 279 map.put("message", message); 280 log("ERROR", clazz, map); 281 } 282 if(LoggerFactory.isLoadedConfig()){ 283 LoggerFactory.getLogger(clazz).error(message); 284 } 285 } 286 } 287 288 public static void systemError(Class clazz, String message) { 289 if (LEVEL <= LEVEL_ERROR) { 290 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 291 Map<String, Object> map = new HashMap<String, Object>(); 292 map.put("message", message); 293 log("ERROR", clazz, map); 294 } 295 if(LoggerFactory.isLoadedConfig()){ 296 LoggerFactory.getLogger(clazz).error(message); 297 } 298 } 299 } 300 301 public static void systemMark(String clazz, String message, Exception exception) { 302 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 303 Map<String, Object> map = new HashMap<String, Object>(); 304 map.put("message", message); 305 map.put("exception", exception); 306 log("MARK", clazz, map); 307 } 308 if(LoggerFactory.isLoadedConfig()){ 309 LoggerFactory.getLogger(clazz).mark(message,exception); 310 } 311 } 312 313 public static void systemMark(Class clazz, String message, Exception exception) { 314 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 315 Map<String, Object> map = new HashMap<String, Object>(); 316 map.put("message", message); 317 map.put("exception", exception); 318 log("MARK", clazz, map); 319 } 320 if(LoggerFactory.isLoadedConfig()){ 321 LoggerFactory.getLogger(clazz).mark(message,exception); 322 } 323 } 324 325 public static void systemMark(String clazz, String message, Object... params) { 326 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 327 String msg = message.replaceAll("\\{\\}", "%s"); 328 String _message = String.format(msg, converParams(params)); 329 Map<String, Object> map = new HashMap<String, Object>(); 330 map.put("message", _message); 331 log("MARK", clazz, map); 332 } 333 if(LoggerFactory.isLoadedConfig()){ 334 LoggerFactory.getLogger(clazz).mark(message,params); 335 } 336 } 337 338 public static void systemMark(Class clazz, String message, Object... params) { 339 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 340 String msg = message.replaceAll("\\{\\}", "%s"); 341 String _message = String.format(msg, converParams(params)); 342 Map<String, Object> map = new HashMap<String, Object>(); 343 map.put("message", _message); 344 log("MARK", clazz, map); 345 } 346 if(LoggerFactory.isLoadedConfig()){ 347 LoggerFactory.getLogger(clazz).mark(message,params); 348 } 349 } 350 351 public static void systemError(String clazz, String message, Exception exception) { 352 if (LEVEL <= LEVEL_ERROR) { 353 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 354 Map<String, Object> map = new HashMap<String, Object>(); 355 map.put("message", message); 356 map.put("exception", exception); 357 log("ERROR", clazz, map); 358 } 359 if(LoggerFactory.isLoadedConfig()){ 360 LoggerFactory.getLogger(clazz).error(message,exception); 361 } 362 } 363 } 364 365 public static void systemError(Class clazz, String message, Exception exception) { 366 if (LEVEL <= LEVEL_ERROR) { 367 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 368 Map<String, Object> map = new HashMap<String, Object>(); 369 map.put("message", message); 370 map.put("exception", exception); 371 log("ERROR", clazz, map); 372 } 373 if(LoggerFactory.isLoadedConfig()){ 374 LoggerFactory.getLogger(clazz).error(message,exception); 375 } 376 } 377 } 378 379 public static void systemError(String clazz, Exception exception) { 380 if (LEVEL <= LEVEL_ERROR) { 381 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 382 Map<String, Object> map = new HashMap<String, Object>(); 383 map.put("message", exception.getMessage()); 384 map.put("exception", exception); 385 log("ERROR", clazz, map); 386 } 387 if(LoggerFactory.isLoadedConfig()){ 388 LoggerFactory.getLogger(clazz).error(exception); 389 } 390 } 391 } 392 393 public static void systemError(Class clazz, Exception exception) { 394 if (LEVEL <= LEVEL_ERROR) { 395 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 396 Map<String, Object> map = new HashMap<String, Object>(); 397 map.put("message", exception.getMessage()); 398 map.put("exception", exception); 399 log("ERROR", clazz, map); 400 } 401 if(LoggerFactory.isLoadedConfig()){ 402 LoggerFactory.getLogger(clazz).error(exception); 403 } 404 } 405 } 406 407 public static void systemError(String clazz, String message, Object... params) { 408 if (LEVEL <= LEVEL_ERROR) { 409 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 410 String msg = message.replaceAll("\\{\\}", "%s"); 411 String _message = String.format(msg, converParams(params)); 412 Map<String, Object> map = new HashMap<String, Object>(); 413 map.put("message", _message); 414 log("ERROR", clazz, map); 415 } 416 if(LoggerFactory.isLoadedConfig()){ 417 LoggerFactory.getLogger(clazz).error(message,params); 418 } 419 } 420 } 421 422 public static void systemError(Class clazz, String message, Object... params) { 423 if (LEVEL <= LEVEL_ERROR) { 424 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 425 String msg = message.replaceAll("\\{\\}", "%s"); 426 String _message = String.format(msg, converParams(params)); 427 Map<String, Object> map = new HashMap<String, Object>(); 428 map.put("message", _message); 429 log("ERROR", clazz, map); 430 } 431 if(LoggerFactory.isLoadedConfig()){ 432 LoggerFactory.getLogger(clazz).error(message,params); 433 } 434 } 435 } 436 437 public static void systemWarn(String clazz, String message) { 438 if (LEVEL <= LEVEL_WARN) { 439 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 440 Map<String, Object> map = new HashMap<String, Object>(); 441 map.put("message", message); 442 log("WARN", clazz, map); 443 } 444 } 445 if(LoggerFactory.isLoadedConfig()){ 446 LoggerFactory.getLogger(clazz).warn(message); 447 } 448 } 449 450 public static void systemWarn(Class clazz, String message) { 451 if (LEVEL <= LEVEL_WARN) { 452 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 453 Map<String, Object> map = new HashMap<String, Object>(); 454 map.put("message", message); 455 log("WARN", clazz, map); 456 } 457 } 458 if(LoggerFactory.isLoadedConfig()){ 459 LoggerFactory.getLogger(clazz).warn(message); 460 } 461 } 462 463 public static void systemWarn(String clazz, String message, Exception exception) { 464 if (LEVEL <= LEVEL_WARN) { 465 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 466 Map<String, Object> map = new HashMap<String, Object>(); 467 map.put("message", message); 468 map.put("exception", exception); 469 log("WARN", clazz, map); 470 } 471 if(LoggerFactory.isLoadedConfig()){ 472 LoggerFactory.getLogger(clazz).warn(message,exception); 473 } 474 } 475 } 476 477 public static void systemWarn(Class clazz, String message, Exception exception) { 478 if (LEVEL <= LEVEL_WARN) { 479 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 480 Map<String, Object> map = new HashMap<String, Object>(); 481 map.put("message", message); 482 map.put("exception", exception); 483 log("WARN", clazz, map); 484 } 485 if(LoggerFactory.isLoadedConfig()){ 486 LoggerFactory.getLogger(clazz).warn(message,exception); 487 } 488 } 489 } 490 491 public static void systemWarn(String clazz, Exception exception) { 492 if (LEVEL <= LEVEL_WARN) { 493 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 494 Map<String, Object> map = new HashMap<String, Object>(); 495 map.put("message", exception.getMessage()); 496 map.put("exception", exception); 497 log("WARN", clazz, map); 498 } 499 if(LoggerFactory.isLoadedConfig()){ 500 LoggerFactory.getLogger(clazz).warn(exception); 501 } 502 } 503 } 504 505 public static void systemWarn(Class clazz, Exception exception) { 506 if (LEVEL <= LEVEL_WARN) { 507 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 508 Map<String, Object> map = new HashMap<String, Object>(); 509 map.put("message", exception.getMessage()); 510 map.put("exception", exception); 511 log("WARN", clazz, map); 512 } 513 if(LoggerFactory.isLoadedConfig()){ 514 LoggerFactory.getLogger(clazz).warn(exception); 515 } 516 } 517 } 518 519 public static void systemWarn(String clazz, String message, Object... params) { 520 if (LEVEL <= LEVEL_WARN) { 521 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 522 String msg = message.replaceAll("\\{\\}", "%s"); 523 String _message = String.format(msg, converParams(params)); 524 Map<String, Object> map = new HashMap<String, Object>(); 525 map.put("message", _message); 526 log("WARN", clazz, map); 527 } 528 if(LoggerFactory.isLoadedConfig()){ 529 LoggerFactory.getLogger(clazz).warn(message,params); 530 } 531 } 532 } 533 534 public static void systemWarn(Class clazz, String message, Object... params) { 535 if (LEVEL <= LEVEL_WARN) { 536 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 537 String msg = message.replaceAll("\\{\\}", "%s"); 538 String _message = String.format(msg, converParams(params)); 539 Map<String, Object> map = new HashMap<String, Object>(); 540 map.put("message", _message); 541 log("WARN", clazz, map); 542 } 543 if(LoggerFactory.isLoadedConfig()){ 544 LoggerFactory.getLogger(clazz).warn(message,params); 545 } 546 } 547 } 548 549 public static void systemInfo(String clazz, String message) { 550 if (LEVEL <= LEVEL_INFO) { 551 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 552 Map<String, Object> map = new HashMap<String, Object>(); 553 map.put("message", message); 554 log("INFO", clazz, map); 555 } 556 if(LoggerFactory.isLoadedConfig()){ 557 LoggerFactory.getLogger(clazz).info(message); 558 } 559 } 560 } 561 562 public static void systemInfo(Class clazz, String message) { 563 if (LEVEL <= LEVEL_INFO) { 564 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 565 Map<String, Object> map = new HashMap<String, Object>(); 566 map.put("message", message); 567 log("INFO", clazz, map); 568 } 569 if(LoggerFactory.isLoadedConfig()){ 570 LoggerFactory.getLogger(clazz).info(message); 571 } 572 } 573 } 574 575 public static void systemInfo(String clazz, String message, Exception exception) { 576 if (LEVEL <= LEVEL_INFO) { 577 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 578 Map<String, Object> map = new HashMap<String, Object>(); 579 map.put("message", message); 580 map.put("exception", exception); 581 log("INFO", clazz, map); 582 } 583 if(LoggerFactory.isLoadedConfig()){ 584 LoggerFactory.getLogger(clazz).info(message,exception); 585 } 586 } 587 } 588 589 public static void systemInfo(Class clazz, String message, Exception exception) { 590 if (LEVEL <= LEVEL_INFO) { 591 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 592 Map<String, Object> map = new HashMap<String, Object>(); 593 map.put("message", message); 594 map.put("exception", exception); 595 log("INFO", clazz, map); 596 } 597 if(LoggerFactory.isLoadedConfig()){ 598 LoggerFactory.getLogger(clazz).info(message,exception); 599 } 600 } 601 } 602 603 public static void systemInfo(String clazz, Exception exception) { 604 if (LEVEL <= LEVEL_INFO) { 605 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 606 Map<String, Object> map = new HashMap<String, Object>(); 607 map.put("message", exception.getMessage()); 608 map.put("exception", exception); 609 log("INFO", clazz, map); 610 } 611 if(LoggerFactory.isLoadedConfig()){ 612 LoggerFactory.getLogger(clazz).info(exception); 613 } 614 } 615 } 616 617 public static void systemInfo(Class clazz, Exception exception) { 618 if (LEVEL <= LEVEL_INFO) { 619 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 620 Map<String, Object> map = new HashMap<String, Object>(); 621 map.put("message", exception.getMessage()); 622 map.put("exception", exception); 623 log("INFO", clazz, map); 624 } 625 if(LoggerFactory.isLoadedConfig()){ 626 LoggerFactory.getLogger(clazz).info(exception); 627 } 628 } 629 } 630 631 public static void systemInfo(String clazz, String message, Object... params) { 632 if (LEVEL <= LEVEL_INFO) { 633 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 634 String msg = message.replaceAll("\\{\\}", "%s"); 635 String _message = String.format(msg, converParams(params)); 636 Map<String, Object> map = new HashMap<String, Object>(); 637 map.put("message", _message); 638 log("INFO", clazz, map); 639 } 640 if(LoggerFactory.isLoadedConfig()){ 641 LoggerFactory.getLogger(clazz).info(message,params); 642 } 643 } 644 } 645 646 public static void systemInfo(Class clazz, String message, Object... params) { 647 if (LEVEL <= LEVEL_INFO) { 648 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 649 String msg = message.replaceAll("\\{\\}", "%s"); 650 String _message = String.format(msg, converParams(params)); 651 Map<String, Object> map = new HashMap<String, Object>(); 652 map.put("message", _message); 653 log("INFO", clazz, map); 654 } 655 if(LoggerFactory.isLoadedConfig()){ 656 LoggerFactory.getLogger(clazz).info(message,params); 657 } 658 } 659 } 660 661 public static void systemDebug(String clazz, String message) { 662 if (LEVEL <= LEVEL_DEBUG) { 663 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 664 Map<String, Object> map = new HashMap<String, Object>(); 665 map.put("message", message); 666 log("DEBUG", clazz, map); 667 } 668 if(LoggerFactory.isLoadedConfig()){ 669 LoggerFactory.getLogger(clazz).debug(message); 670 } 671 } 672 } 673 674 public static void systemDebug(Class clazz, String message) { 675 if (LEVEL <= LEVEL_DEBUG) { 676 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 677 Map<String, Object> map = new HashMap<String, Object>(); 678 map.put("message", message); 679 log("DEBUG", clazz, map); 680 } 681 if(LoggerFactory.isLoadedConfig()){ 682 LoggerFactory.getLogger(clazz).debug(message); 683 } 684 } 685 } 686 687 public static void systemDebug(String clazz, String message, Exception exception) { 688 if (LEVEL <= LEVEL_DEBUG) { 689 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 690 Map<String, Object> map = new HashMap<String, Object>(); 691 map.put("message", message); 692 map.put("exception", exception); 693 log("DEBUG", clazz, map); 694 } 695 if(LoggerFactory.isLoadedConfig()){ 696 LoggerFactory.getLogger(clazz).debug(exception); 697 } 698 } 699 } 700 701 public static void systemDebug(Class clazz, String message, Exception exception) { 702 if (LEVEL <= LEVEL_DEBUG) { 703 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 704 Map<String, Object> map = new HashMap<String, Object>(); 705 map.put("message", message); 706 map.put("exception", exception); 707 log("DEBUG", clazz, map); 708 } 709 if(LoggerFactory.isLoadedConfig()){ 710 LoggerFactory.getLogger(clazz).debug(exception); 711 } 712 } 713 } 714 715 public static void systemDebug(String clazz, Exception exception) { 716 if (LEVEL <= LEVEL_DEBUG) { 717 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 718 Map<String, Object> map = new HashMap<String, Object>(); 719 map.put("message", exception.getMessage()); 720 map.put("exception", exception); 721 log("DEBUG", clazz, map); 722 } 723 if(LoggerFactory.isLoadedConfig()){ 724 LoggerFactory.getLogger(clazz).debug(exception); 725 } 726 } 727 } 728 729 public static void systemDebug(Class clazz, Exception exception) { 730 if (LEVEL <= LEVEL_DEBUG) { 731 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 732 Map<String, Object> map = new HashMap<String, Object>(); 733 map.put("message", exception.getMessage()); 734 map.put("exception", exception); 735 log("DEBUG", clazz, map); 736 } 737 if(LoggerFactory.isLoadedConfig()){ 738 LoggerFactory.getLogger(clazz).debug(exception); 739 } 740 } 741 } 742 743 public static void systemDebug(String clazz, String message, Object... params) { 744 if (LEVEL <= LEVEL_DEBUG) { 745 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 746 String msg = message.replaceAll("\\{\\}", "%s"); 747 String _message = String.format(msg, converParams(params)); 748 Map<String, Object> map = new HashMap<String, Object>(); 749 map.put("message", _message); 750 log("DEBUG", clazz, map); 751 } 752 if(LoggerFactory.isLoadedConfig()){ 753 LoggerFactory.getLogger(clazz).debug(message,params); 754 } 755 } 756 } 757 758 public static void systemDebug(Class clazz, String message, Object... params) { 759 if (LEVEL <= LEVEL_DEBUG) { 760 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 761 String msg = message.replaceAll("\\{\\}", "%s"); 762 String _message = String.format(msg, converParams(params)); 763 Map<String, Object> map = new HashMap<String, Object>(); 764 map.put("message", _message); 765 log("DEBUG", clazz, map); 766 } 767 if(LoggerFactory.isLoadedConfig()){ 768 LoggerFactory.getLogger(clazz).debug(message,params); 769 } 770 } 771 } 772 773 private static void log(String level, String clazzName, Map<String, Object> map) { 774 map.put("level", level); 775 776 StackTraceElement[] threadStacks = Thread.currentThread().getStackTrace(); 777 for (StackTraceElement stack : threadStacks) { 778 String className = stack.getClassName(); 779 if (className.equals(clazzName)) { 780 String fileName = stack.getFileName(); 781 String methodName = stack.getMethodName(); 782 int lineNumber = stack.getLineNumber(); 783 map.put("log_file_name", fileName); 784 map.put("log_class_name", className); 785 map.put("log_method_name", methodName); 786 map.put("log_line_number", lineNumber); 787 break; 788 } 789 } 790 791 Thread currentThread = Thread.currentThread(); 792 long threadId = currentThread.getId(); 793 String threadName = currentThread.getName(); 794 795 Date logTime = new Date(); 796 map.put("log_time", logTime); 797 map.put("log_time_format", LogRecord.getLogTimeFormat().format(logTime)); 798 map.put("log_thread_id", threadId); 799 map.put("log_thread_name", threadName); 800 801 Exception exception = (Exception) map.get("exception"); 802 if (exception != null) { 803 StringWriter sw = new StringWriter(); 804 exception.printStackTrace(new PrintWriter(sw)); 805 String exceptionAsString = sw.toString(); 806 String exceptionClassName = exception.getClass().getName(); 807 String exceptionMessage = exception.getMessage(); 808 StackTraceElement[] stacks = exception.getStackTrace(); 809 String fileName = null; 810 String className = null; 811 String methodName = null; 812 int lineNumber = 0; 813 for (StackTraceElement stack : stacks) { 814 fileName = stack.getFileName(); 815 className = stack.getClassName(); 816 methodName = stack.getMethodName(); 817 lineNumber = stack.getLineNumber(); 818 break; 819 } 820 map.put("stack_trace", sw.toString()); 821 map.put("exception_class_name", exceptionClassName); 822 map.put("exception_message", exceptionMessage); 823 map.put("issued_file_name", fileName); 824 map.put("issued_class_name", className); 825 map.put("issued_method_name", methodName); 826 map.put("issued_line_number", lineNumber); 827 } 828 829 if (!map.containsKey("stack_trace")) 830 map.put("stack_trace", ""); 831 832 if (!map.containsKey("stack_trace_single")) 833 map.put("stack_trace_single", ""); 834 835 if (!map.containsKey("log_file_name")) 836 map.put("log_file_name", "Unknown"); 837 838 if (!map.containsKey("log_class_name")) 839 map.put("log_class_name", clazzName); 840 841 if (!map.containsKey("log_method_name")) 842 map.put("log_method_name", "Unknown"); 843 844 if (!map.containsKey("log_line_number")) 845 map.put("log_line_number", 0); 846 847 map.remove("exception"); 848 849 String msg = LogRecord.format(DEFAULT_MESSAGE_FORMAT, map); 850 System.out.println(msg); 851 } 852 853 private static void log(String level, Class clazz, Map<String, Object> map) { 854 log(level, clazz.getName(),map); 855 } 856 857 protected void log(Integer _level, Map<String, Object> map) { 858 String levelStr = null; 859 String log4jMethodName = null; 860 switch (_level) { 861 case 0: 862 levelStr = "DEBUG"; 863 break; 864 case 1: 865 levelStr = "INFO"; 866 break; 867 case 2: 868 levelStr = "WARN"; 869 break; 870 case 3: 871 levelStr = "ERROR"; 872 break; 873 case 4: 874 levelStr = "MARK"; 875 break; 876 default: 877 break; 878 } 879 map.put("level", levelStr); 880 881 StackTraceElement[] threadStacks = Thread.currentThread().getStackTrace(); 882 for (StackTraceElement stack : threadStacks) { 883 String className = stack.getClassName(); 884 if (className.equals(clazzName)) { 885 String fileName = stack.getFileName(); 886 String methodName = stack.getMethodName(); 887 int lineNumber = stack.getLineNumber(); 888 map.put("log_file_name", fileName); 889 map.put("log_class_name", className); 890 map.put("log_method_name", methodName); 891 map.put("log_line_number", lineNumber); 892 break; 893 } 894 } 895 896 Thread currentThread = Thread.currentThread(); 897 long threadId = currentThread.getId(); 898 String threadName = currentThread.getName(); 899 900 Date logTime = new Date(); 901 map.put("log_time", logTime); 902 map.put("log_time_format", LogRecord.getLogTimeFormat().format(logTime)); 903 map.put("log_thread_id", threadId); 904 map.put("log_thread_name", threadName); 905 906 Exception exception = (Exception) map.get("exception"); 907 if (exception != null) { 908 StringWriter sw = new StringWriter(); 909 exception.printStackTrace(new PrintWriter(sw)); 910 String exceptionAsString = sw.toString(); 911 String exceptionClassName = exception.getClass().getName(); 912 String exceptionMessage = exception.getMessage(); 913 StackTraceElement[] stacks = exception.getStackTrace(); 914 String fileName = null; 915 String className = null; 916 String methodName = null; 917 int lineNumber = 0; 918 for (StackTraceElement stack : stacks) { 919 fileName = stack.getFileName(); 920 className = stack.getClassName(); 921 methodName = stack.getMethodName(); 922 lineNumber = stack.getLineNumber(); 923 break; 924 } 925 map.put("stack_trace", sw.toString()); 926 map.put("stack_trace_single", toSingle(sw.toString())); 927 map.put("exception_class_name", exceptionClassName); 928 map.put("exception_message", exceptionMessage); 929 map.put("exception_message_single", toSingle(exceptionMessage)); 930 map.put("issued_file_name", fileName); 931 map.put("issued_class_name", className); 932 map.put("issued_method_name", methodName); 933 map.put("issued_line_number", lineNumber); 934 } 935 936 if (map.containsKey("message")) { 937 map.put("message_single", toSingle((String) map.get("message"))); 938 } 939 940 if (!map.containsKey("stack_trace")) 941 map.put("stack_trace", ""); 942 943 if (!map.containsKey("stack_trace_single")) 944 map.put("stack_trace_single", ""); 945 946 if (!map.containsKey("log_file_name")) 947 map.put("log_file_name", "Unknown"); 948 949 if (!map.containsKey("log_class_name")) 950 map.put("log_class_name", clazzName); 951 952 if (!map.containsKey("log_method_name")) 953 map.put("log_method_name", "Unknown"); 954 955 if (!map.containsKey("log_line_number")) 956 map.put("log_line_number", 0); 957 958 map.put("id", CommonTools.generateId(16)); 959 map.put("created_at", CommonTools.generateId(16)); 960 map.put("hostname", hostname); 961 962 map.remove("exception"); 963 964 String logClassName = String.format("%s.%s", map.get("log_class_name"), map.get("log_method_name")); 965 boolean passEnableRegex = checkClassNameEnableRegex(logClassName); 966 boolean passDisableRegex = !checkClassNameDisableRegex(logClassName); 967 968 if (passEnableRegex && passDisableRegex) { 969 LogRecord.add(map); 970 } 971 } 972 973 private boolean checkClassNameEnableRegex(String logClassName) { 974 if (!ConfigProperties.isBlank(classNameEnableRegex)) { 975 return logClassName.matches(classNameEnableRegex); 976 } 977 return true; 978 } 979 980 private boolean checkClassNameDisableRegex(String logClassName) { 981 if (!ConfigProperties.isBlank(classNameDisableRegex)) { 982 return logClassName.matches(classNameDisableRegex); 983 } 984 return false; 985 } 986 987 public String getClazzName() { 988 return clazzName; 989 } 990 991 private String toSingle(String text) { 992 return CodeEscape.escapeToSingleLineForCsv(text); 993 } 994 995 private static String[] converParams(Object... params){ 996 if(params == null) 997 return new String[]{"null"}; 998 999 String[] stringParams = new String[params.length]; 1000 for (int i=0;i<params.length;i++){ 1001 stringParams[i]= params[i] + ""; 1002 } 1003 return stringParams; 1004 } 1005 1006}