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 try{ 119 if (LEVEL <= LEVEL_DEBUG) { 120 Map<String, Object> map = new HashMap<String, Object>(); 121 map.put("message", message); 122 log(LEVEL_DEBUG, map); 123 } 124 }catch(Exception e){ 125 e.printStackTrace(); 126 } 127 } 128 129 public void debug(String message, Exception exception) { 130 try{ 131 if (LEVEL <= LEVEL_DEBUG) { 132 Map<String, Object> map = new HashMap<String, Object>(); 133 map.put("message", message); 134 map.put("exception", exception); 135 log(LEVEL_DEBUG, map); 136 } 137 }catch(Exception e){ 138 e.printStackTrace(); 139 } 140 } 141 142 public void debug(Exception exception) { 143 try{ 144 if (LEVEL <= LEVEL_DEBUG) { 145 Map<String, Object> map = new HashMap<String, Object>(); 146 map.put("message", exception.getMessage()); 147 map.put("exception", exception); 148 log(LEVEL_DEBUG, map); 149 } 150 }catch(Exception e){ 151 e.printStackTrace(); 152 } 153 } 154 155 public void debug(String message, Object... params) { 156 try{ 157 if (LEVEL <= LEVEL_DEBUG) { 158 String msg = (message + "").replaceAll("\\{\\}", "%s"); 159 String _message = String.format(msg, converParams(params)); 160 Map<String, Object> map = new HashMap<String, Object>(); 161 map.put("message", _message); 162 log(LEVEL_DEBUG, map); 163 } 164 }catch(Exception e){ 165 e.printStackTrace(); 166 } 167 } 168 169 public void info(String message) { 170 try{ 171 if (LEVEL <= LEVEL_INFO) { 172 Map<String, Object> map = new HashMap<String, Object>(); 173 map.put("message", message); 174 log(LEVEL_INFO, map); 175 } 176 }catch(Exception e){ 177 e.printStackTrace(); 178 } 179 } 180 181 public void info(String message, Exception exception) { 182 try{ 183 if (LEVEL <= LEVEL_INFO) { 184 Map<String, Object> map = new HashMap<String, Object>(); 185 map.put("message", message); 186 map.put("exception", exception); 187 log(LEVEL_INFO, map); 188 } 189 }catch(Exception e){ 190 e.printStackTrace(); 191 } 192 } 193 194 public void info(Exception exception) { 195 try{ 196 if (LEVEL <= LEVEL_INFO) { 197 Map<String, Object> map = new HashMap<String, Object>(); 198 map.put("message", exception.getMessage()); 199 map.put("exception", exception); 200 log(LEVEL_INFO, map); 201 } 202 }catch(Exception e){ 203 e.printStackTrace(); 204 } 205 } 206 207 public void info(String message, Object... params) { 208 try{ 209 if (LEVEL <= LEVEL_INFO) { 210 String msg = (message + "").replaceAll("\\{\\}", "%s"); 211 String _message = String.format(msg, converParams(params)); 212 Map<String, Object> map = new HashMap<String, Object>(); 213 map.put("message", _message); 214 log(LEVEL_INFO, map); 215 } 216 }catch(Exception e){ 217 e.printStackTrace(); 218 } 219 } 220 221 public void warn(String message) { 222 try{ 223 if (LEVEL <= LEVEL_WARN) { 224 Map<String, Object> map = new HashMap<String, Object>(); 225 map.put("message", message); 226 log(LEVEL_WARN, map); 227 } 228 }catch(Exception e){ 229 e.printStackTrace(); 230 } 231 } 232 233 public void warn(String message, Exception exception) { 234 try{ 235 if (LEVEL <= LEVEL_WARN) { 236 Map<String, Object> map = new HashMap<String, Object>(); 237 map.put("message", message); 238 map.put("exception", exception); 239 log(LEVEL_WARN, map); 240 } 241 }catch(Exception e){ 242 e.printStackTrace(); 243 } 244 } 245 246 public void warn(Exception exception) { 247 try{ 248 if (LEVEL <= LEVEL_WARN) { 249 Map<String, Object> map = new HashMap<String, Object>(); 250 map.put("message", exception.getMessage()); 251 map.put("exception", exception); 252 log(LEVEL_WARN, map); 253 } 254 }catch(Exception e){ 255 e.printStackTrace(); 256 } 257 } 258 259 public void warn(String message, Object... params) { 260 try{ 261 if (LEVEL <= LEVEL_WARN) { 262 String msg = (message + "").replaceAll("\\{\\}", "%s"); 263 String _message = String.format(msg, converParams(params)); 264 Map<String, Object> map = new HashMap<String, Object>(); 265 map.put("message", _message); 266 log(LEVEL_WARN, map); 267 } 268 }catch(Exception e){ 269 e.printStackTrace(); 270 } 271 } 272 273 public void error(String message) { 274 try{ 275 if (LEVEL <= LEVEL_ERROR) { 276 Map<String, Object> map = new HashMap<String, Object>(); 277 map.put("message", message); 278 log(LEVEL_ERROR, map); 279 } 280 }catch(Exception e){ 281 e.printStackTrace(); 282 } 283 } 284 285 public void error(String message, Exception exception) { 286 try{ 287 if (LEVEL <= LEVEL_ERROR) { 288 Map<String, Object> map = new HashMap<String, Object>(); 289 map.put("message", message); 290 map.put("exception", exception); 291 log(LEVEL_ERROR, map); 292 } 293 }catch(Exception e){ 294 e.printStackTrace(); 295 } 296 } 297 298 public void error(Exception exception) { 299 try{ 300 if (LEVEL <= LEVEL_ERROR) { 301 Map<String, Object> map = new HashMap<String, Object>(); 302 map.put("message", exception.getMessage()); 303 map.put("exception", exception); 304 log(LEVEL_ERROR, map); 305 } 306 }catch(Exception e){ 307 e.printStackTrace(); 308 } 309 } 310 311 public void error(String message, Object... params) { 312 try{ 313 if (LEVEL <= LEVEL_ERROR) { 314 String msg = (message + "").replaceAll("\\{\\}", "%s"); 315 String _message = String.format(msg, converParams(params)); 316 Map<String, Object> map = new HashMap<String, Object>(); 317 map.put("message", _message); 318 log(LEVEL_ERROR, map); 319 } 320 }catch(Exception e){ 321 e.printStackTrace(); 322 } 323 } 324 325 public void mark(String message) { 326 try{ 327 Map<String, Object> map = new HashMap<String, Object>(); 328 map.put("message", message); 329 log(LEVEL_MARK, map); 330 }catch(Exception e){ 331 e.printStackTrace(); 332 } 333 } 334 335 public void mark(String message, Object... params) { 336 try{ 337 String msg = (message + "").replaceAll("\\{\\}", "%s"); 338 String _message = String.format(msg, converParams(params)); 339 Map<String, Object> map = new HashMap<String, Object>(); 340 map.put("message", _message); 341 log(LEVEL_MARK, map); 342 }catch(Exception e){ 343 e.printStackTrace(); 344 } 345 } 346 347 public static void systemError(String clazz, String message) { 348 try{ 349 if (LEVEL <= LEVEL_ERROR) { 350 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 351 Map<String, Object> map = new HashMap<String, Object>(); 352 map.put("message", message); 353 log("ERROR", clazz, map); 354 } 355 if(LoggerFactory.isLoadedConfig()){ 356 LoggerFactory.getLogger(clazz).error(message); 357 } 358 } 359 }catch(Exception e){ 360 e.printStackTrace(); 361 } 362 } 363 364 public static void systemError(Class clazz, String message) { 365 if (LEVEL <= LEVEL_ERROR) { 366 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 367 Map<String, Object> map = new HashMap<String, Object>(); 368 map.put("message", message); 369 log("ERROR", clazz, map); 370 } 371 if(LoggerFactory.isLoadedConfig()){ 372 LoggerFactory.getLogger(clazz).error(message); 373 } 374 } 375 } 376 377 public static void systemMark(String clazz, String message, Exception exception) { 378 try{ 379 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 380 Map<String, Object> map = new HashMap<String, Object>(); 381 map.put("message", message); 382 map.put("exception", exception); 383 log("MARK", clazz, map); 384 } 385 if(LoggerFactory.isLoadedConfig()){ 386 LoggerFactory.getLogger(clazz).mark(message,exception); 387 } 388 }catch(Exception e){ 389 e.printStackTrace(); 390 } 391 } 392 393 public static void systemMark(Class clazz, String message, Exception exception) { 394 try{ 395 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 396 Map<String, Object> map = new HashMap<String, Object>(); 397 map.put("message", message); 398 map.put("exception", exception); 399 log("MARK", clazz, map); 400 } 401 if(LoggerFactory.isLoadedConfig()){ 402 LoggerFactory.getLogger(clazz).mark(message,exception); 403 } 404 }catch(Exception e){ 405 e.printStackTrace(); 406 } 407 } 408 409 public static void systemMark(String clazz, String message, Object... params) { 410 try{ 411 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 412 String msg = (message + "").replaceAll("\\{\\}", "%s"); 413 String _message = String.format(msg, converParams(params)); 414 Map<String, Object> map = new HashMap<String, Object>(); 415 map.put("message", _message); 416 log("MARK", clazz, map); 417 } 418 if(LoggerFactory.isLoadedConfig()){ 419 LoggerFactory.getLogger(clazz).mark(message,params); 420 } 421 }catch(Exception e){ 422 e.printStackTrace(); 423 } 424 } 425 426 public static void systemMark(Class clazz, String message, Object... params) { 427 try{ 428 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 429 String msg = (message + "").replaceAll("\\{\\}", "%s"); 430 String _message = String.format(msg, converParams(params)); 431 Map<String, Object> map = new HashMap<String, Object>(); 432 map.put("message", _message); 433 log("MARK", clazz, map); 434 } 435 if(LoggerFactory.isLoadedConfig()){ 436 LoggerFactory.getLogger(clazz).mark(message,params); 437 } 438 }catch(Exception e){ 439 e.printStackTrace(); 440 } 441 } 442 443 public static void systemError(String clazz, String message, Exception exception) { 444 try{ 445 if (LEVEL <= LEVEL_ERROR) { 446 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 447 Map<String, Object> map = new HashMap<String, Object>(); 448 map.put("message", message); 449 map.put("exception", exception); 450 log("ERROR", clazz, map); 451 } 452 if(LoggerFactory.isLoadedConfig()){ 453 LoggerFactory.getLogger(clazz).error(message,exception); 454 } 455 } 456 }catch(Exception e){ 457 e.printStackTrace(); 458 } 459 } 460 461 public static void systemError(Class clazz, String message, Exception exception) { 462 try{ 463 if (LEVEL <= LEVEL_ERROR) { 464 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 465 Map<String, Object> map = new HashMap<String, Object>(); 466 map.put("message", message); 467 map.put("exception", exception); 468 log("ERROR", clazz, map); 469 } 470 if(LoggerFactory.isLoadedConfig()){ 471 LoggerFactory.getLogger(clazz).error(message,exception); 472 } 473 } 474 }catch(Exception e){ 475 e.printStackTrace(); 476 } 477 } 478 479 public static void systemError(String clazz, Exception exception) { 480 try{ 481 if (LEVEL <= LEVEL_ERROR) { 482 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 483 Map<String, Object> map = new HashMap<String, Object>(); 484 map.put("message", exception.getMessage()); 485 map.put("exception", exception); 486 log("ERROR", clazz, map); 487 } 488 if(LoggerFactory.isLoadedConfig()){ 489 LoggerFactory.getLogger(clazz).error(exception); 490 } 491 } 492 }catch(Exception e){ 493 e.printStackTrace(); 494 } 495 } 496 497 public static void systemError(Class clazz, Exception exception) { 498 try{ 499 if (LEVEL <= LEVEL_ERROR) { 500 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 501 Map<String, Object> map = new HashMap<String, Object>(); 502 map.put("message", exception.getMessage()); 503 map.put("exception", exception); 504 log("ERROR", clazz, map); 505 } 506 if(LoggerFactory.isLoadedConfig()){ 507 LoggerFactory.getLogger(clazz).error(exception); 508 } 509 } 510 }catch(Exception e){ 511 e.printStackTrace(); 512 } 513 } 514 515 public static void systemError(String clazz, String message, Object... params) { 516 try{ 517 if (LEVEL <= LEVEL_ERROR) { 518 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 519 String msg = (message + "").replaceAll("\\{\\}", "%s"); 520 String _message = String.format(msg, converParams(params)); 521 Map<String, Object> map = new HashMap<String, Object>(); 522 map.put("message", _message); 523 log("ERROR", clazz, map); 524 } 525 if(LoggerFactory.isLoadedConfig()){ 526 LoggerFactory.getLogger(clazz).error(message,params); 527 } 528 } 529 }catch(Exception e){ 530 e.printStackTrace(); 531 } 532 } 533 534 public static void systemError(Class clazz, String message, Object... params) { 535 try{ 536 if (LEVEL <= LEVEL_ERROR) { 537 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 538 String msg = (message + "").replaceAll("\\{\\}", "%s"); 539 String _message = String.format(msg, converParams(params)); 540 Map<String, Object> map = new HashMap<String, Object>(); 541 map.put("message", _message); 542 log("ERROR", clazz, map); 543 } 544 if(LoggerFactory.isLoadedConfig()){ 545 LoggerFactory.getLogger(clazz).error(message,params); 546 } 547 } 548 }catch(Exception e){ 549 e.printStackTrace(); 550 } 551 } 552 553 public static void systemWarn(String clazz, String message) { 554 try{ 555 if (LEVEL <= LEVEL_WARN) { 556 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 557 Map<String, Object> map = new HashMap<String, Object>(); 558 map.put("message", message); 559 log("WARN", clazz, map); 560 } 561 } 562 if(LoggerFactory.isLoadedConfig()){ 563 LoggerFactory.getLogger(clazz).warn(message); 564 } 565 }catch(Exception e){ 566 e.printStackTrace(); 567 } 568 } 569 570 public static void systemWarn(Class clazz, String message) { 571 try{ 572 if (LEVEL <= LEVEL_WARN) { 573 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 574 Map<String, Object> map = new HashMap<String, Object>(); 575 map.put("message", message); 576 log("WARN", clazz, map); 577 } 578 } 579 if(LoggerFactory.isLoadedConfig()){ 580 LoggerFactory.getLogger(clazz).warn(message); 581 } 582 }catch(Exception e){ 583 e.printStackTrace(); 584 } 585 } 586 587 public static void systemWarn(String clazz, String message, Exception exception) { 588 try{ 589 if (LEVEL <= LEVEL_WARN) { 590 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 591 Map<String, Object> map = new HashMap<String, Object>(); 592 map.put("message", message); 593 map.put("exception", exception); 594 log("WARN", clazz, map); 595 } 596 if(LoggerFactory.isLoadedConfig()){ 597 LoggerFactory.getLogger(clazz).warn(message,exception); 598 } 599 } 600 }catch(Exception e){ 601 e.printStackTrace(); 602 } 603 } 604 605 public static void systemWarn(Class clazz, String message, Exception exception) { 606 try{ 607 if (LEVEL <= LEVEL_WARN) { 608 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 609 Map<String, Object> map = new HashMap<String, Object>(); 610 map.put("message", message); 611 map.put("exception", exception); 612 log("WARN", clazz, map); 613 } 614 if(LoggerFactory.isLoadedConfig()){ 615 LoggerFactory.getLogger(clazz).warn(message,exception); 616 } 617 } 618 }catch(Exception e){ 619 e.printStackTrace(); 620 } 621 } 622 623 public static void systemWarn(String clazz, Exception exception) { 624 try{ 625 if (LEVEL <= LEVEL_WARN) { 626 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 627 Map<String, Object> map = new HashMap<String, Object>(); 628 map.put("message", exception.getMessage()); 629 map.put("exception", exception); 630 log("WARN", clazz, map); 631 } 632 if(LoggerFactory.isLoadedConfig()){ 633 LoggerFactory.getLogger(clazz).warn(exception); 634 } 635 } 636 }catch(Exception e){ 637 e.printStackTrace(); 638 } 639 } 640 641 public static void systemWarn(Class clazz, Exception exception) { 642 try{ 643 if (LEVEL <= LEVEL_WARN) { 644 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 645 Map<String, Object> map = new HashMap<String, Object>(); 646 map.put("message", exception.getMessage()); 647 map.put("exception", exception); 648 log("WARN", clazz, map); 649 } 650 if(LoggerFactory.isLoadedConfig()){ 651 LoggerFactory.getLogger(clazz).warn(exception); 652 } 653 } 654 }catch(Exception e){ 655 e.printStackTrace(); 656 } 657 } 658 659 public static void systemWarn(String clazz, String message, Object... params) { 660 try{ 661 if (LEVEL <= LEVEL_WARN) { 662 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 663 String msg = (message + "").replaceAll("\\{\\}", "%s"); 664 String _message = String.format(msg, converParams(params)); 665 Map<String, Object> map = new HashMap<String, Object>(); 666 map.put("message", _message); 667 log("WARN", clazz, map); 668 } 669 if(LoggerFactory.isLoadedConfig()){ 670 LoggerFactory.getLogger(clazz).warn(message,params); 671 } 672 } 673 }catch(Exception e){ 674 e.printStackTrace(); 675 } 676 } 677 678 public static void systemWarn(Class clazz, String message, Object... params) { 679 try{ 680 if (LEVEL <= LEVEL_WARN) { 681 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 682 String msg = (message + "").replaceAll("\\{\\}", "%s"); 683 String _message = String.format(msg, converParams(params)); 684 Map<String, Object> map = new HashMap<String, Object>(); 685 map.put("message", _message); 686 log("WARN", clazz, map); 687 } 688 if(LoggerFactory.isLoadedConfig()){ 689 LoggerFactory.getLogger(clazz).warn(message,params); 690 } 691 } 692 }catch(Exception e){ 693 e.printStackTrace(); 694 } 695 } 696 697 public static void systemInfo(String clazz, String message) { 698 try{ 699 if (LEVEL <= LEVEL_INFO) { 700 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 701 Map<String, Object> map = new HashMap<String, Object>(); 702 map.put("message", message); 703 log("INFO", clazz, map); 704 } 705 if(LoggerFactory.isLoadedConfig()){ 706 LoggerFactory.getLogger(clazz).info(message); 707 } 708 } 709 }catch(Exception e){ 710 e.printStackTrace(); 711 } 712 } 713 714 public static void systemInfo(Class clazz, String message) { 715 try{ 716 if (LEVEL <= LEVEL_INFO) { 717 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 718 Map<String, Object> map = new HashMap<String, Object>(); 719 map.put("message", message); 720 log("INFO", clazz, map); 721 } 722 if(LoggerFactory.isLoadedConfig()){ 723 LoggerFactory.getLogger(clazz).info(message); 724 } 725 } 726 }catch(Exception e){ 727 e.printStackTrace(); 728 } 729 } 730 731 public static void systemInfo(String clazz, String message, Exception exception) { 732 try{ 733 if (LEVEL <= LEVEL_INFO) { 734 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 735 Map<String, Object> map = new HashMap<String, Object>(); 736 map.put("message", message); 737 map.put("exception", exception); 738 log("INFO", clazz, map); 739 } 740 if(LoggerFactory.isLoadedConfig()){ 741 LoggerFactory.getLogger(clazz).info(message,exception); 742 } 743 } 744 }catch(Exception e){ 745 e.printStackTrace(); 746 } 747 } 748 749 public static void systemInfo(Class clazz, String message, Exception exception) { 750 try{ 751 if (LEVEL <= LEVEL_INFO) { 752 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 753 Map<String, Object> map = new HashMap<String, Object>(); 754 map.put("message", message); 755 map.put("exception", exception); 756 log("INFO", clazz, map); 757 } 758 if(LoggerFactory.isLoadedConfig()){ 759 LoggerFactory.getLogger(clazz).info(message,exception); 760 } 761 } 762 }catch(Exception e){ 763 e.printStackTrace(); 764 } 765 } 766 767 public static void systemInfo(String clazz, Exception exception) { 768 try{ 769 if (LEVEL <= LEVEL_INFO) { 770 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 771 Map<String, Object> map = new HashMap<String, Object>(); 772 map.put("message", exception.getMessage()); 773 map.put("exception", exception); 774 log("INFO", clazz, map); 775 } 776 if(LoggerFactory.isLoadedConfig()){ 777 LoggerFactory.getLogger(clazz).info(exception); 778 } 779 } 780 }catch(Exception e){ 781 e.printStackTrace(); 782 } 783 } 784 785 public static void systemInfo(Class clazz, Exception exception) { 786 try{ 787 if (LEVEL <= LEVEL_INFO) { 788 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 789 Map<String, Object> map = new HashMap<String, Object>(); 790 map.put("message", exception.getMessage()); 791 map.put("exception", exception); 792 log("INFO", clazz, map); 793 } 794 if(LoggerFactory.isLoadedConfig()){ 795 LoggerFactory.getLogger(clazz).info(exception); 796 } 797 } 798 }catch(Exception e){ 799 e.printStackTrace(); 800 } 801 } 802 803 public static void systemInfo(String clazz, String message, Object... params) { 804 try{ 805 if (LEVEL <= LEVEL_INFO) { 806 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 807 String msg = (message + "").replaceAll("\\{\\}", "%s"); 808 String _message = String.format(msg, converParams(params)); 809 Map<String, Object> map = new HashMap<String, Object>(); 810 map.put("message", _message); 811 log("INFO", clazz, map); 812 } 813 if(LoggerFactory.isLoadedConfig()){ 814 LoggerFactory.getLogger(clazz).info(message,params); 815 } 816 } 817 }catch(Exception e){ 818 e.printStackTrace(); 819 } 820 } 821 822 public static void systemInfo(Class clazz, String message, Object... params) { 823 try{ 824 if (LEVEL <= LEVEL_INFO) { 825 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 826 String msg = (message + "").replaceAll("\\{\\}", "%s"); 827 String _message = String.format(msg, converParams(params)); 828 Map<String, Object> map = new HashMap<String, Object>(); 829 map.put("message", _message); 830 log("INFO", clazz, map); 831 } 832 if(LoggerFactory.isLoadedConfig()){ 833 LoggerFactory.getLogger(clazz).info(message,params); 834 } 835 } 836 }catch(Exception e){ 837 e.printStackTrace(); 838 } 839 } 840 841 public static void systemDebug(String clazz, String message) { 842 try{ 843 if (LEVEL <= LEVEL_DEBUG) { 844 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 845 Map<String, Object> map = new HashMap<String, Object>(); 846 map.put("message", message); 847 log("DEBUG", clazz, map); 848 } 849 if(LoggerFactory.isLoadedConfig()){ 850 LoggerFactory.getLogger(clazz).debug(message); 851 } 852 } 853 }catch(Exception e){ 854 e.printStackTrace(); 855 } 856 } 857 858 public static void systemDebug(Class clazz, String message) { 859 try{ 860 if (LEVEL <= LEVEL_DEBUG) { 861 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 862 Map<String, Object> map = new HashMap<String, Object>(); 863 map.put("message", message); 864 log("DEBUG", clazz, map); 865 } 866 if(LoggerFactory.isLoadedConfig()){ 867 LoggerFactory.getLogger(clazz).debug(message); 868 } 869 } 870 }catch(Exception e){ 871 e.printStackTrace(); 872 } 873 } 874 875 public static void systemDebug(String clazz, String message, Exception exception) { 876 try{ 877 if (LEVEL <= LEVEL_DEBUG) { 878 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 879 Map<String, Object> map = new HashMap<String, Object>(); 880 map.put("message", message); 881 map.put("exception", exception); 882 log("DEBUG", clazz, map); 883 } 884 if(LoggerFactory.isLoadedConfig()){ 885 LoggerFactory.getLogger(clazz).debug(exception); 886 } 887 } 888 }catch(Exception e){ 889 e.printStackTrace(); 890 } 891 } 892 893 public static void systemDebug(Class clazz, String message, Exception exception) { 894 try{ 895 if (LEVEL <= LEVEL_DEBUG) { 896 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 897 Map<String, Object> map = new HashMap<String, Object>(); 898 map.put("message", message); 899 map.put("exception", exception); 900 log("DEBUG", clazz, map); 901 } 902 if(LoggerFactory.isLoadedConfig()){ 903 LoggerFactory.getLogger(clazz).debug(exception); 904 } 905 } 906 }catch(Exception e){ 907 e.printStackTrace(); 908 } 909 } 910 911 public static void systemDebug(String clazz, Exception exception) { 912 try{ 913 if (LEVEL <= LEVEL_DEBUG) { 914 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 915 Map<String, Object> map = new HashMap<String, Object>(); 916 map.put("message", exception.getMessage()); 917 map.put("exception", exception); 918 log("DEBUG", clazz, map); 919 } 920 if(LoggerFactory.isLoadedConfig()){ 921 LoggerFactory.getLogger(clazz).debug(exception); 922 } 923 } 924 }catch(Exception e){ 925 e.printStackTrace(); 926 } 927 } 928 929 public static void systemDebug(Class clazz, Exception exception) { 930 try{ 931 if (LEVEL <= LEVEL_DEBUG) { 932 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 933 Map<String, Object> map = new HashMap<String, Object>(); 934 map.put("message", exception.getMessage()); 935 map.put("exception", exception); 936 log("DEBUG", clazz, map); 937 } 938 if(LoggerFactory.isLoadedConfig()){ 939 LoggerFactory.getLogger(clazz).debug(exception); 940 } 941 } 942 }catch(Exception e){ 943 e.printStackTrace(); 944 } 945 } 946 947 public static void systemDebug(String clazz, String message, Object... params) { 948 try{ 949 if (LEVEL <= LEVEL_DEBUG) { 950 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 951 String msg = (message + "").replaceAll("\\{\\}", "%s"); 952 String _message = String.format(msg, converParams(params)); 953 Map<String, Object> map = new HashMap<String, Object>(); 954 map.put("message", _message); 955 log("DEBUG", clazz, map); 956 } 957 if(LoggerFactory.isLoadedConfig()){ 958 LoggerFactory.getLogger(clazz).debug(message,params); 959 } 960 } 961 }catch(Exception e){ 962 e.printStackTrace(); 963 } 964 } 965 966 public static void systemDebug(Class clazz, String message, Object... params) { 967 try{ 968 if (LEVEL <= LEVEL_DEBUG) { 969 if(LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()){ 970 String msg = (message + "").replaceAll("\\{\\}", "%s"); 971 String _message = String.format(msg, converParams(params)); 972 Map<String, Object> map = new HashMap<String, Object>(); 973 map.put("message", _message); 974 log("DEBUG", clazz, map); 975 } 976 if(LoggerFactory.isLoadedConfig()){ 977 LoggerFactory.getLogger(clazz).debug(message,params); 978 } 979 } 980 }catch(Exception e){ 981 e.printStackTrace(); 982 } 983 } 984 985 private static void log(String level, String clazzName, Map<String, Object> map) { 986 map.put("level", level); 987 988 StackTraceElement[] threadStacks = Thread.currentThread().getStackTrace(); 989 for (StackTraceElement stack : threadStacks) { 990 String className = stack.getClassName(); 991 if (className.equals(clazzName)) { 992 String fileName = stack.getFileName(); 993 String methodName = stack.getMethodName(); 994 int lineNumber = stack.getLineNumber(); 995 map.put("log_file_name", fileName); 996 map.put("log_class_name", className); 997 map.put("log_method_name", methodName); 998 map.put("log_line_number", lineNumber); 999 break; 1000 } 1001 } 1002 1003 Thread currentThread = Thread.currentThread(); 1004 long threadId = currentThread.getId(); 1005 String threadName = currentThread.getName(); 1006 1007 Date logTime = new Date(); 1008 map.put("log_time", logTime); 1009 map.put("log_time_format", LogRecord.getLogTimeFormat().format(logTime)); 1010 map.put("log_thread_id", threadId); 1011 map.put("log_thread_name", threadName); 1012 1013 Exception exception = (Exception) map.get("exception"); 1014 if (exception != null) { 1015 StringWriter sw = new StringWriter(); 1016 exception.printStackTrace(new PrintWriter(sw)); 1017 String exceptionAsString = sw.toString(); 1018 String exceptionClassName = exception.getClass().getName(); 1019 String exceptionMessage = exception.getMessage(); 1020 StackTraceElement[] stacks = exception.getStackTrace(); 1021 String fileName = null; 1022 String className = null; 1023 String methodName = null; 1024 int lineNumber = 0; 1025 for (StackTraceElement stack : stacks) { 1026 fileName = stack.getFileName(); 1027 className = stack.getClassName(); 1028 methodName = stack.getMethodName(); 1029 lineNumber = stack.getLineNumber(); 1030 break; 1031 } 1032 map.put("stack_trace", sw.toString()); 1033 map.put("exception_class_name", exceptionClassName); 1034 map.put("exception_message", exceptionMessage); 1035 map.put("issued_file_name", fileName); 1036 map.put("issued_class_name", className); 1037 map.put("issued_method_name", methodName); 1038 map.put("issued_line_number", lineNumber); 1039 } 1040 1041 if (!map.containsKey("stack_trace")) 1042 map.put("stack_trace", ""); 1043 1044 if (!map.containsKey("stack_trace_single")) 1045 map.put("stack_trace_single", ""); 1046 1047 if (!map.containsKey("log_file_name")) 1048 map.put("log_file_name", "Unknown"); 1049 1050 if (!map.containsKey("log_class_name")) 1051 map.put("log_class_name", clazzName); 1052 1053 if (!map.containsKey("log_method_name")) 1054 map.put("log_method_name", "Unknown"); 1055 1056 if (!map.containsKey("log_line_number")) 1057 map.put("log_line_number", 0); 1058 1059 map.remove("exception"); 1060 1061 String msg = LogRecord.format(DEFAULT_MESSAGE_FORMAT, map); 1062 System.out.println(msg); 1063 } 1064 1065 private static void log(String level, Class clazz, Map<String, Object> map) { 1066 log(level, clazz.getName(),map); 1067 } 1068 1069 protected void log(Integer _level, Map<String, Object> map) { 1070 String levelStr = null; 1071 String log4jMethodName = null; 1072 switch (_level) { 1073 case 0: 1074 levelStr = "DEBUG"; 1075 break; 1076 case 1: 1077 levelStr = "INFO"; 1078 break; 1079 case 2: 1080 levelStr = "WARN"; 1081 break; 1082 case 3: 1083 levelStr = "ERROR"; 1084 break; 1085 case 4: 1086 levelStr = "MARK"; 1087 break; 1088 default: 1089 break; 1090 } 1091 map.put("level", levelStr); 1092 1093 StackTraceElement[] threadStacks = Thread.currentThread().getStackTrace(); 1094 for (StackTraceElement stack : threadStacks) { 1095 String className = stack.getClassName(); 1096 if (className.equals(clazzName)) { 1097 String fileName = stack.getFileName(); 1098 String methodName = stack.getMethodName(); 1099 int lineNumber = stack.getLineNumber(); 1100 map.put("log_file_name", fileName); 1101 map.put("log_class_name", className); 1102 map.put("log_method_name", methodName); 1103 map.put("log_line_number", lineNumber); 1104 break; 1105 } 1106 } 1107 1108 Thread currentThread = Thread.currentThread(); 1109 long threadId = currentThread.getId(); 1110 String threadName = currentThread.getName(); 1111 1112 Date logTime = new Date(); 1113 map.put("log_time", logTime); 1114 map.put("log_time_format", LogRecord.getLogTimeFormat().format(logTime)); 1115 map.put("log_thread_id", threadId); 1116 map.put("log_thread_name", threadName); 1117 1118 Exception exception = (Exception) map.get("exception"); 1119 if (exception != null) { 1120 StringWriter sw = new StringWriter(); 1121 exception.printStackTrace(new PrintWriter(sw)); 1122 String exceptionAsString = sw.toString(); 1123 String exceptionClassName = exception.getClass().getName(); 1124 String exceptionMessage = exception.getMessage(); 1125 StackTraceElement[] stacks = exception.getStackTrace(); 1126 String fileName = null; 1127 String className = null; 1128 String methodName = null; 1129 int lineNumber = 0; 1130 for (StackTraceElement stack : stacks) { 1131 fileName = stack.getFileName(); 1132 className = stack.getClassName(); 1133 methodName = stack.getMethodName(); 1134 lineNumber = stack.getLineNumber(); 1135 break; 1136 } 1137 map.put("stack_trace", sw.toString()); 1138 map.put("stack_trace_single", toSingle(sw.toString())); 1139 map.put("exception_class_name", exceptionClassName); 1140 map.put("exception_message", exceptionMessage); 1141 map.put("exception_message_single", toSingle(exceptionMessage)); 1142 map.put("issued_file_name", fileName); 1143 map.put("issued_class_name", className); 1144 map.put("issued_method_name", methodName); 1145 map.put("issued_line_number", lineNumber); 1146 } 1147 1148 if (map.containsKey("message")) { 1149 map.put("message_single", toSingle((String) map.get("message"))); 1150 } 1151 1152 if (!map.containsKey("stack_trace")) 1153 map.put("stack_trace", ""); 1154 1155 if (!map.containsKey("stack_trace_single")) 1156 map.put("stack_trace_single", ""); 1157 1158 if (!map.containsKey("log_file_name")) 1159 map.put("log_file_name", "Unknown"); 1160 1161 if (!map.containsKey("log_class_name")) 1162 map.put("log_class_name", clazzName); 1163 1164 if (!map.containsKey("log_method_name")) 1165 map.put("log_method_name", "Unknown"); 1166 1167 if (!map.containsKey("log_line_number")) 1168 map.put("log_line_number", 0); 1169 1170 map.put("id", CommonTools.generateId(16)); 1171 map.put("created_at", CommonTools.generateId(16)); 1172 map.put("hostname", hostname); 1173 1174 map.remove("exception"); 1175 1176 String logClassName = String.format("%s.%s", map.get("log_class_name"), map.get("log_method_name")); 1177 boolean passEnableRegex = checkClassNameEnableRegex(logClassName); 1178 boolean passDisableRegex = !checkClassNameDisableRegex(logClassName); 1179 1180 if (passEnableRegex && passDisableRegex) { 1181 LogRecord.add(map); 1182 } 1183 } 1184 1185 private boolean checkClassNameEnableRegex(String logClassName) { 1186 if (!ConfigProperties.isBlank(classNameEnableRegex)) { 1187 return logClassName.matches(classNameEnableRegex); 1188 } 1189 return true; 1190 } 1191 1192 private boolean checkClassNameDisableRegex(String logClassName) { 1193 if (!ConfigProperties.isBlank(classNameDisableRegex)) { 1194 return logClassName.matches(classNameDisableRegex); 1195 } 1196 return false; 1197 } 1198 1199 public String getClazzName() { 1200 return clazzName; 1201 } 1202 1203 private String toSingle(String text) { 1204 return CodeEscape.escapeToSingleLineForCsv(text); 1205 } 1206 1207 private static String[] converParams(Object... params){ 1208 if(params == null) 1209 return new String[]{"null"}; 1210 1211 String[] stringParams = new String[params.length]; 1212 for (int i=0;i<params.length;i++){ 1213 stringParams[i]= params[i] + ""; 1214 } 1215 return stringParams; 1216 } 1217 1218}