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        public static void setLevel(String _level) {
092                if (checkLevel(_level)) {
093                        setLevel(LEVEL_LIST.indexOf(_level.toUpperCase()));
094                }
095        }
096
097        public 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(Object obj) {
118                try {
119                        if (LEVEL <= LEVEL_DEBUG) {
120                                Map<String, Object> map = new HashMap<String, Object>();
121                                map.put("message", obj + "");
122                                log(LEVEL_DEBUG, map);
123                        }
124                } catch (Exception e) {
125                        e.printStackTrace();
126                        System.err.println(e.getMessage());
127                }
128        }
129
130        public void debug(String message) {
131                try {
132                        if (LEVEL <= LEVEL_DEBUG) {
133                                Map<String, Object> map = new HashMap<String, Object>();
134                                map.put("message", message);
135                                log(LEVEL_DEBUG, map);
136                        }
137                } catch (Exception e) {
138                        e.printStackTrace();
139                        System.err.println(e.getMessage());
140                }
141        }
142
143        public void debug(String message, Exception exception) {
144                try {
145                        if (LEVEL <= LEVEL_DEBUG) {
146                                Map<String, Object> map = new HashMap<String, Object>();
147                                map.put("message", message);
148                                map.put("exception", exception);
149                                log(LEVEL_DEBUG, map);
150                        }
151                } catch (Exception e) {
152                        e.printStackTrace();
153                        System.err.println(e.getMessage());
154                }
155        }
156
157        public void debug(Exception exception) {
158                try {
159                        if (LEVEL <= LEVEL_DEBUG) {
160                                Map<String, Object> map = new HashMap<String, Object>();
161                                map.put("message", exception.getMessage());
162                                map.put("exception", exception);
163                                log(LEVEL_DEBUG, map);
164                        }
165                } catch (Exception e) {
166                        e.printStackTrace();
167                        System.err.println(e.getMessage());
168                }
169        }
170
171        public void debug(String message, Object... params) {
172                try {
173                        if (LEVEL <= LEVEL_DEBUG) {
174                                String msg = (message + "").replaceAll("\\{\\}", "%s");
175                                String _message = String.format(msg, converParams(params));
176                                Map<String, Object> map = new HashMap<String, Object>();
177                                map.put("message", _message);
178                                log(LEVEL_DEBUG, map);
179                        }
180                } catch (Exception e) {
181                        e.printStackTrace();
182                        System.err.println(e.getMessage());
183                }
184        }
185
186        public void debug(String message, Object obj) {
187                try {
188                        if (LEVEL <= LEVEL_DEBUG) {
189                                String msg = (message + "").replaceAll("\\{\\}", "%s");
190                                Object[] params = new Object[] { obj + "" };
191                                String _message = String.format(msg, converParams(params));
192                                Map<String, Object> map = new HashMap<String, Object>();
193                                map.put("message", _message);
194                                log(LEVEL_DEBUG, map);
195                        }
196                } catch (Exception e) {
197                        e.printStackTrace();
198                        System.err.println(e.getMessage());
199                }
200        }
201
202        public void info(String message) {
203                try {
204                        if (LEVEL <= LEVEL_INFO) {
205                                Map<String, Object> map = new HashMap<String, Object>();
206                                map.put("message", message);
207                                log(LEVEL_INFO, map);
208                        }
209                } catch (Exception e) {
210                        e.printStackTrace();
211                        System.err.println(e.getMessage());
212                }
213        }
214
215        public void info(Object obj) {
216                try {
217                        if (LEVEL <= LEVEL_INFO) {
218                                Map<String, Object> map = new HashMap<String, Object>();
219                                map.put("message", obj + "");
220                                log(LEVEL_INFO, map);
221                        }
222                } catch (Exception e) {
223                        e.printStackTrace();
224                        System.err.println(e.getMessage());
225                }
226        }
227
228        public void info(String message, Exception exception) {
229                try {
230                        if (LEVEL <= LEVEL_INFO) {
231                                Map<String, Object> map = new HashMap<String, Object>();
232                                map.put("message", message);
233                                map.put("exception", exception);
234                                log(LEVEL_INFO, map);
235                        }
236                } catch (Exception e) {
237                        e.printStackTrace();
238                        System.err.println(e.getMessage());
239                }
240        }
241
242        public void info(Exception exception) {
243                try {
244                        if (LEVEL <= LEVEL_INFO) {
245                                Map<String, Object> map = new HashMap<String, Object>();
246                                map.put("message", exception.getMessage());
247                                map.put("exception", exception);
248                                log(LEVEL_INFO, map);
249                        }
250                } catch (Exception e) {
251                        e.printStackTrace();
252                        System.err.println(e.getMessage());
253                }
254        }
255
256        public void info(String message, Object... params) {
257                try {
258                        if (LEVEL <= LEVEL_INFO) {
259                                String msg = (message + "").replaceAll("\\{\\}", "%s");
260                                String _message = String.format(msg, converParams(params));
261                                Map<String, Object> map = new HashMap<String, Object>();
262                                map.put("message", _message);
263                                log(LEVEL_INFO, map);
264                        }
265                } catch (Exception e) {
266                        e.printStackTrace();
267                        System.err.println(e.getMessage());
268                }
269        }
270
271        public void info(String message, Object obj) {
272                try {
273                        if (LEVEL <= LEVEL_INFO) {
274                                String msg = (message + "").replaceAll("\\{\\}", "%s");
275                                Object[] params = new Object[] { obj + "" };
276                                String _message = String.format(msg, converParams(params));
277                                Map<String, Object> map = new HashMap<String, Object>();
278                                map.put("message", _message);
279                                log(LEVEL_INFO, map);
280                        }
281                } catch (Exception e) {
282                        e.printStackTrace();
283                        System.err.println(e.getMessage());
284                }
285        }
286
287        public void warn(String message) {
288                try {
289                        if (LEVEL <= LEVEL_WARN) {
290                                Map<String, Object> map = new HashMap<String, Object>();
291                                map.put("message", message);
292                                log(LEVEL_WARN, map);
293                        }
294                } catch (Exception e) {
295                        e.printStackTrace();
296                        System.err.println(e.getMessage());
297                }
298        }
299
300        public void warn(Object obj) {
301                try {
302                        if (LEVEL <= LEVEL_WARN) {
303                                Map<String, Object> map = new HashMap<String, Object>();
304                                map.put("message", obj + "");
305                                log(LEVEL_WARN, map);
306                        }
307                } catch (Exception e) {
308                        e.printStackTrace();
309                        System.err.println(e.getMessage());
310                }
311        }
312
313        public void warn(String message, Exception exception) {
314                try {
315                        if (LEVEL <= LEVEL_WARN) {
316                                Map<String, Object> map = new HashMap<String, Object>();
317                                map.put("message", message);
318                                map.put("exception", exception);
319                                log(LEVEL_WARN, map);
320                        }
321                } catch (Exception e) {
322                        e.printStackTrace();
323                        System.err.println(e.getMessage());
324                }
325        }
326
327        public void warn(Exception exception) {
328                try {
329                        if (LEVEL <= LEVEL_WARN) {
330                                Map<String, Object> map = new HashMap<String, Object>();
331                                map.put("message", exception.getMessage());
332                                map.put("exception", exception);
333                                log(LEVEL_WARN, map);
334                        }
335                } catch (Exception e) {
336                        e.printStackTrace();
337                        System.err.println(e.getMessage());
338                }
339        }
340
341        public void warn(String message, Object... params) {
342                try {
343                        if (LEVEL <= LEVEL_WARN) {
344                                String msg = (message + "").replaceAll("\\{\\}", "%s");
345                                String _message = String.format(msg, converParams(params));
346                                Map<String, Object> map = new HashMap<String, Object>();
347                                map.put("message", _message);
348                                log(LEVEL_WARN, map);
349                        }
350                } catch (Exception e) {
351                        e.printStackTrace();
352                        System.err.println(e.getMessage());
353                }
354        }
355
356        public void warn(String message, Object obj) {
357                try {
358                        if (LEVEL <= LEVEL_WARN) {
359                                String msg = (message + "").replaceAll("\\{\\}", "%s");
360                                Object[] params = new Object[] { obj + "" };
361                                String _message = String.format(msg, converParams(params));
362                                Map<String, Object> map = new HashMap<String, Object>();
363                                map.put("message", _message);
364                                log(LEVEL_WARN, map);
365                        }
366                } catch (Exception e) {
367                        e.printStackTrace();
368                        System.err.println(e.getMessage());
369                }
370        }
371
372        public void error(String message) {
373                try {
374                        if (LEVEL <= LEVEL_ERROR) {
375                                Map<String, Object> map = new HashMap<String, Object>();
376                                map.put("message", message);
377                                log(LEVEL_ERROR, map);
378                        }
379                } catch (Exception e) {
380                        e.printStackTrace();
381                        System.err.println(e.getMessage());
382                }
383        }
384
385        public void error(Object obj) {
386                try {
387                        if (LEVEL <= LEVEL_ERROR) {
388                                Map<String, Object> map = new HashMap<String, Object>();
389                                map.put("message", obj + "");
390                                log(LEVEL_ERROR, map);
391                        }
392                } catch (Exception e) {
393                        e.printStackTrace();
394                        System.err.println(e.getMessage());
395                }
396        }
397
398        public void error(String message, Exception exception) {
399                try {
400                        if (LEVEL <= LEVEL_ERROR) {
401                                Map<String, Object> map = new HashMap<String, Object>();
402                                map.put("message", message);
403                                map.put("exception", exception);
404                                log(LEVEL_ERROR, map);
405                        }
406                } catch (Exception e) {
407                        e.printStackTrace();
408                        System.err.println(e.getMessage());
409                }
410        }
411
412        public void error(Exception exception) {
413                try {
414                        if (LEVEL <= LEVEL_ERROR) {
415                                Map<String, Object> map = new HashMap<String, Object>();
416                                map.put("message", exception.getMessage());
417                                map.put("exception", exception);
418                                log(LEVEL_ERROR, map);
419                        }
420                } catch (Exception e) {
421                        e.printStackTrace();
422                        System.err.println(e.getMessage());
423                }
424        }
425
426        public void error(String message, Object... params) {
427                try {
428                        if (LEVEL <= LEVEL_ERROR) {
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(LEVEL_ERROR, map);
434                        }
435                } catch (Exception e) {
436                        e.printStackTrace();
437                        System.err.println(e.getMessage());
438                }
439        }
440
441        public void error(String message, Object obj) {
442                try {
443                        if (LEVEL <= LEVEL_ERROR) {
444                                String msg = (message + "").replaceAll("\\{\\}", "%s");
445                                Object[] params = new Object[] { obj + "" };
446                                String _message = String.format(msg, converParams(params));
447                                Map<String, Object> map = new HashMap<String, Object>();
448                                map.put("message", _message);
449                                log(LEVEL_ERROR, map);
450                        }
451                } catch (Exception e) {
452                        e.printStackTrace();
453                        System.err.println(e.getMessage());
454                }
455        }
456
457        public void mark(String message) {
458                try {
459                        Map<String, Object> map = new HashMap<String, Object>();
460                        map.put("message", message);
461                        log(LEVEL_MARK, map);
462                } catch (Exception e) {
463                        e.printStackTrace();
464                        System.err.println(e.getMessage());
465                }
466        }
467
468        public void mark(Object obj) {
469                try {
470                        Map<String, Object> map = new HashMap<String, Object>();
471                        map.put("message", obj + "");
472                        log(LEVEL_MARK, map);
473                } catch (Exception e) {
474                        e.printStackTrace();
475                        System.err.println(e.getMessage());
476                }
477        }
478
479        public void mark(String message, Object... params) {
480                try {
481                        String msg = (message + "").replaceAll("\\{\\}", "%s");
482                        String _message = String.format(msg, converParams(params));
483                        Map<String, Object> map = new HashMap<String, Object>();
484                        map.put("message", _message);
485                        log(LEVEL_MARK, map);
486                } catch (Exception e) {
487                        e.printStackTrace();
488                        System.err.println(e.getMessage());
489                }
490        }
491
492        public void mark(String message, Object obj) {
493                try {
494                        String msg = (message + "").replaceAll("\\{\\}", "%s");
495                        Object[] params = new Object[] { obj + "" };
496                        String _message = String.format(msg, converParams(params));
497                        Map<String, Object> map = new HashMap<String, Object>();
498                        map.put("message", _message);
499                        log(LEVEL_MARK, map);
500                } catch (Exception e) {
501                        e.printStackTrace();
502                        System.err.println(e.getMessage());
503                }
504        }
505
506        public static void systemError(String clazz, String message) {
507                try {
508                        if (LEVEL <= LEVEL_ERROR) {
509                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
510                                        Map<String, Object> map = new HashMap<String, Object>();
511                                        map.put("message", message);
512                                        log("ERROR", clazz, map);
513                                }
514                                if (LoggerFactory.isLoadedConfig()) {
515                                        LoggerFactory.getLogger(clazz).error(message);
516                                }
517                        }
518                } catch (Exception e) {
519                        e.printStackTrace();
520                        System.err.println(e.getMessage());
521                }
522        }
523
524        public static void systemError(String clazz, Object obj) {
525                try {
526                        if (LEVEL <= LEVEL_ERROR) {
527                                String message = obj + "";
528                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
529                                        Map<String, Object> map = new HashMap<String, Object>();
530                                        map.put("message", message);
531                                        log("ERROR", clazz, map);
532                                }
533                                if (LoggerFactory.isLoadedConfig()) {
534                                        LoggerFactory.getLogger(clazz).error(message);
535                                }
536                        }
537                } catch (Exception e) {
538                        e.printStackTrace();
539                        System.err.println(e.getMessage());
540                }
541        }
542
543        public static void systemError(Class clazz, String message) {
544                if (LEVEL <= LEVEL_ERROR) {
545                        if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
546                                Map<String, Object> map = new HashMap<String, Object>();
547                                map.put("message", message);
548                                log("ERROR", clazz, map);
549                        }
550                        if (LoggerFactory.isLoadedConfig()) {
551                                LoggerFactory.getLogger(clazz).error(message);
552                        }
553                }
554        }
555
556        public static void systemError(Class clazz, Object obj) {
557                if (LEVEL <= LEVEL_ERROR) {
558                        String message = obj + "";
559                        if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
560                                Map<String, Object> map = new HashMap<String, Object>();
561                                map.put("message", message);
562                                log("ERROR", clazz, map);
563                        }
564                        if (LoggerFactory.isLoadedConfig()) {
565                                LoggerFactory.getLogger(clazz).error(message);
566                        }
567                }
568        }
569
570        public static void systemMark(String clazz, String message, Exception exception) {
571                try {
572                        if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
573                                Map<String, Object> map = new HashMap<String, Object>();
574                                map.put("message", message);
575                                map.put("exception", exception);
576                                log("MARK", clazz, map);
577                        }
578                        if (LoggerFactory.isLoadedConfig()) {
579                                LoggerFactory.getLogger(clazz).mark(message, exception);
580                        }
581                } catch (Exception e) {
582                        e.printStackTrace();
583                        System.err.println(e.getMessage());
584                }
585        }
586
587        public static void systemMark(Class clazz, String message, Exception exception) {
588                try {
589                        if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
590                                Map<String, Object> map = new HashMap<String, Object>();
591                                map.put("message", message);
592                                map.put("exception", exception);
593                                log("MARK", clazz, map);
594                        }
595                        if (LoggerFactory.isLoadedConfig()) {
596                                LoggerFactory.getLogger(clazz).mark(message, exception);
597                        }
598                } catch (Exception e) {
599                        e.printStackTrace();
600                        System.err.println(e.getMessage());
601                }
602        }
603
604        public static void systemMark(String clazz, String message, Object... params) {
605                try {
606                        if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
607                                String msg = (message + "").replaceAll("\\{\\}", "%s");
608                                String _message = String.format(msg, converParams(params));
609                                Map<String, Object> map = new HashMap<String, Object>();
610                                map.put("message", _message);
611                                log("MARK", clazz, map);
612                        }
613                        if (LoggerFactory.isLoadedConfig()) {
614                                LoggerFactory.getLogger(clazz).mark(message, params);
615                        }
616                } catch (Exception e) {
617                        e.printStackTrace();
618                        System.err.println(e.getMessage());
619                }
620        }
621
622        public static void systemMark(String clazz, String message, Object obj) {
623                try {
624                        Object[] params = new Object[] { obj + "" };
625                        if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
626                                String msg = (message + "").replaceAll("\\{\\}", "%s");
627                                String _message = String.format(msg, converParams(params));
628                                Map<String, Object> map = new HashMap<String, Object>();
629                                map.put("message", _message);
630                                log("MARK", clazz, map);
631                        }
632                        if (LoggerFactory.isLoadedConfig()) {
633                                LoggerFactory.getLogger(clazz).mark(message, params);
634                        }
635                } catch (Exception e) {
636                        e.printStackTrace();
637                        System.err.println(e.getMessage());
638                }
639        }
640
641        public static void systemMark(String clazz, Object obj) {
642                try {
643                        String message = obj + "";
644                        if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
645                                Map<String, Object> map = new HashMap<String, Object>();
646                                map.put("message", message);
647                                log("MARK", clazz, map);
648                        }
649                        if (LoggerFactory.isLoadedConfig()) {
650                                LoggerFactory.getLogger(clazz).mark(message);
651                        }
652                } catch (Exception e) {
653                        e.printStackTrace();
654                        System.err.println(e.getMessage());
655                }
656        }
657
658        public static void systemMark(Class clazz, String message, Object... params) {
659                try {
660                        if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
661                                String msg = (message + "").replaceAll("\\{\\}", "%s");
662                                String _message = String.format(msg, converParams(params));
663                                Map<String, Object> map = new HashMap<String, Object>();
664                                map.put("message", _message);
665                                log("MARK", clazz, map);
666                        }
667                        if (LoggerFactory.isLoadedConfig()) {
668                                LoggerFactory.getLogger(clazz).mark(message, params);
669                        }
670                } catch (Exception e) {
671                        e.printStackTrace();
672                        System.err.println(e.getMessage());
673                }
674        }
675
676        public static void systemMark(Class clazz, String message, Object obj) {
677                try {
678                        Object[] params = new Object[] { obj + "" };
679                        if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
680                                String msg = (message + "").replaceAll("\\{\\}", "%s");
681                                String _message = String.format(msg, converParams(params));
682                                Map<String, Object> map = new HashMap<String, Object>();
683                                map.put("message", _message);
684                                log("MARK", clazz, map);
685                        }
686                        if (LoggerFactory.isLoadedConfig()) {
687                                LoggerFactory.getLogger(clazz).mark(message, params);
688                        }
689                } catch (Exception e) {
690                        e.printStackTrace();
691                        System.err.println(e.getMessage());
692                }
693        }
694
695        public static void systemMark(Class clazz, Object obj) {
696                try {
697                        String message = obj + "";
698                        if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
699                                Map<String, Object> map = new HashMap<String, Object>();
700                                map.put("message", message);
701                                log("MARK", clazz, map);
702                        }
703                        if (LoggerFactory.isLoadedConfig()) {
704                                LoggerFactory.getLogger(clazz).mark(message);
705                        }
706                } catch (Exception e) {
707                        e.printStackTrace();
708                        System.err.println(e.getMessage());
709                }
710        }
711
712        public static void systemError(String clazz, String message, Exception exception) {
713                try {
714                        if (LEVEL <= LEVEL_ERROR) {
715                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
716                                        Map<String, Object> map = new HashMap<String, Object>();
717                                        map.put("message", message);
718                                        map.put("exception", exception);
719                                        log("ERROR", clazz, map);
720                                }
721                                if (LoggerFactory.isLoadedConfig()) {
722                                        LoggerFactory.getLogger(clazz).error(message, exception);
723                                }
724                        }
725                } catch (Exception e) {
726                        e.printStackTrace();
727                        System.err.println(e.getMessage());
728                }
729        }
730
731        public static void systemError(Class clazz, String message, Exception exception) {
732                try {
733                        if (LEVEL <= LEVEL_ERROR) {
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("ERROR", clazz, map);
739                                }
740                                if (LoggerFactory.isLoadedConfig()) {
741                                        LoggerFactory.getLogger(clazz).error(message, exception);
742                                }
743                        }
744                } catch (Exception e) {
745                        e.printStackTrace();
746                        System.err.println(e.getMessage());
747                }
748        }
749
750        public static void systemError(String clazz, Exception exception) {
751                try {
752                        if (LEVEL <= LEVEL_ERROR) {
753                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
754                                        Map<String, Object> map = new HashMap<String, Object>();
755                                        map.put("message", exception.getMessage());
756                                        map.put("exception", exception);
757                                        log("ERROR", clazz, map);
758                                }
759                                if (LoggerFactory.isLoadedConfig()) {
760                                        LoggerFactory.getLogger(clazz).error(exception);
761                                }
762                        }
763                } catch (Exception e) {
764                        e.printStackTrace();
765                        System.err.println(e.getMessage());
766                }
767        }
768
769        public static void systemError(Class clazz, Exception exception) {
770                try {
771                        if (LEVEL <= LEVEL_ERROR) {
772                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
773                                        Map<String, Object> map = new HashMap<String, Object>();
774                                        map.put("message", exception.getMessage());
775                                        map.put("exception", exception);
776                                        log("ERROR", clazz, map);
777                                }
778                                if (LoggerFactory.isLoadedConfig()) {
779                                        LoggerFactory.getLogger(clazz).error(exception);
780                                }
781                        }
782                } catch (Exception e) {
783                        e.printStackTrace();
784                        System.err.println(e.getMessage());
785                }
786        }
787
788        public static void systemError(String clazz, String message, Object... params) {
789                try {
790                        if (LEVEL <= LEVEL_ERROR) {
791                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
792                                        String msg = (message + "").replaceAll("\\{\\}", "%s");
793                                        String _message = String.format(msg, converParams(params));
794                                        Map<String, Object> map = new HashMap<String, Object>();
795                                        map.put("message", _message);
796                                        log("ERROR", clazz, map);
797                                }
798                                if (LoggerFactory.isLoadedConfig()) {
799                                        LoggerFactory.getLogger(clazz).error(message, params);
800                                }
801                        }
802                } catch (Exception e) {
803                        e.printStackTrace();
804                        System.err.println(e.getMessage());
805                }
806        }
807
808        public static void systemError(String clazz, String message, Object obj) {
809                try {
810                        if (LEVEL <= LEVEL_ERROR) {
811                                Object[] params = new Object[] { obj + "" };
812                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
813                                        String msg = (message + "").replaceAll("\\{\\}", "%s");
814                                        String _message = String.format(msg, converParams(params));
815                                        Map<String, Object> map = new HashMap<String, Object>();
816                                        map.put("message", _message);
817                                        log("ERROR", clazz, map);
818                                }
819                                if (LoggerFactory.isLoadedConfig()) {
820                                        LoggerFactory.getLogger(clazz).error(message, params);
821                                }
822                        }
823                } catch (Exception e) {
824                        e.printStackTrace();
825                        System.err.println(e.getMessage());
826                }
827        }
828
829        public static void systemError(Class clazz, String message, Object... params) {
830                try {
831                        if (LEVEL <= LEVEL_ERROR) {
832                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
833                                        String msg = (message + "").replaceAll("\\{\\}", "%s");
834                                        String _message = String.format(msg, converParams(params));
835                                        Map<String, Object> map = new HashMap<String, Object>();
836                                        map.put("message", _message);
837                                        log("ERROR", clazz, map);
838                                }
839                                if (LoggerFactory.isLoadedConfig()) {
840                                        LoggerFactory.getLogger(clazz).error(message, params);
841                                }
842                        }
843                } catch (Exception e) {
844                        e.printStackTrace();
845                        System.err.println(e.getMessage());
846                }
847        }
848
849        public static void systemError(Class clazz, String message, Object obj) {
850                try {
851                        if (LEVEL <= LEVEL_ERROR) {
852                                Object[] params = new Object[] { obj + "" };
853                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
854                                        String msg = (message + "").replaceAll("\\{\\}", "%s");
855                                        String _message = String.format(msg, converParams(params));
856                                        Map<String, Object> map = new HashMap<String, Object>();
857                                        map.put("message", _message);
858                                        log("ERROR", clazz, map);
859                                }
860                                if (LoggerFactory.isLoadedConfig()) {
861                                        LoggerFactory.getLogger(clazz).error(message, params);
862                                }
863                        }
864                } catch (Exception e) {
865                        e.printStackTrace();
866                        System.err.println(e.getMessage());
867                }
868        }
869
870        public static void systemWarn(String clazz, String message) {
871                try {
872                        if (LEVEL <= LEVEL_WARN) {
873                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
874                                        Map<String, Object> map = new HashMap<String, Object>();
875                                        map.put("message", message);
876                                        log("WARN", clazz, map);
877                                }
878                        }
879                        if (LoggerFactory.isLoadedConfig()) {
880                                LoggerFactory.getLogger(clazz).warn(message);
881                        }
882                } catch (Exception e) {
883                        e.printStackTrace();
884                        System.err.println(e.getMessage());
885                }
886        }
887
888        public static void systemWarn(Class clazz, String message) {
889                try {
890                        if (LEVEL <= LEVEL_WARN) {
891                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
892                                        Map<String, Object> map = new HashMap<String, Object>();
893                                        map.put("message", message);
894                                        log("WARN", clazz, map);
895                                }
896                        }
897                        if (LoggerFactory.isLoadedConfig()) {
898                                LoggerFactory.getLogger(clazz).warn(message);
899                        }
900                } catch (Exception e) {
901                        e.printStackTrace();
902                        System.err.println(e.getMessage());
903                }
904        }
905
906        public static void systemWarn(String clazz, String message, Exception exception) {
907                try {
908                        if (LEVEL <= LEVEL_WARN) {
909                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
910                                        Map<String, Object> map = new HashMap<String, Object>();
911                                        map.put("message", message);
912                                        map.put("exception", exception);
913                                        log("WARN", clazz, map);
914                                }
915                                if (LoggerFactory.isLoadedConfig()) {
916                                        LoggerFactory.getLogger(clazz).warn(message, exception);
917                                }
918                        }
919                } catch (Exception e) {
920                        e.printStackTrace();
921                        System.err.println(e.getMessage());
922                }
923        }
924
925        public static void systemWarn(Class clazz, String message, Exception exception) {
926                try {
927                        if (LEVEL <= LEVEL_WARN) {
928                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
929                                        Map<String, Object> map = new HashMap<String, Object>();
930                                        map.put("message", message);
931                                        map.put("exception", exception);
932                                        log("WARN", clazz, map);
933                                }
934                                if (LoggerFactory.isLoadedConfig()) {
935                                        LoggerFactory.getLogger(clazz).warn(message, exception);
936                                }
937                        }
938                } catch (Exception e) {
939                        e.printStackTrace();
940                        System.err.println(e.getMessage());
941                }
942        }
943
944        public static void systemWarn(String clazz, Exception exception) {
945                try {
946                        if (LEVEL <= LEVEL_WARN) {
947                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
948                                        Map<String, Object> map = new HashMap<String, Object>();
949                                        map.put("message", exception.getMessage());
950                                        map.put("exception", exception);
951                                        log("WARN", clazz, map);
952                                }
953                                if (LoggerFactory.isLoadedConfig()) {
954                                        LoggerFactory.getLogger(clazz).warn(exception);
955                                }
956                        }
957                } catch (Exception e) {
958                        e.printStackTrace();
959                        System.err.println(e.getMessage());
960                }
961        }
962
963        public static void systemWarn(Class clazz, Exception exception) {
964                try {
965                        if (LEVEL <= LEVEL_WARN) {
966                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
967                                        Map<String, Object> map = new HashMap<String, Object>();
968                                        map.put("message", exception.getMessage());
969                                        map.put("exception", exception);
970                                        log("WARN", clazz, map);
971                                }
972                                if (LoggerFactory.isLoadedConfig()) {
973                                        LoggerFactory.getLogger(clazz).warn(exception);
974                                }
975                        }
976                } catch (Exception e) {
977                        e.printStackTrace();
978                        System.err.println(e.getMessage());
979                }
980        }
981
982        public static void systemWarn(String clazz, String message, Object... params) {
983                try {
984                        if (LEVEL <= LEVEL_WARN) {
985                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
986                                        String msg = (message + "").replaceAll("\\{\\}", "%s");
987                                        String _message = String.format(msg, converParams(params));
988                                        Map<String, Object> map = new HashMap<String, Object>();
989                                        map.put("message", _message);
990                                        log("WARN", clazz, map);
991                                }
992                                if (LoggerFactory.isLoadedConfig()) {
993                                        LoggerFactory.getLogger(clazz).warn(message, params);
994                                }
995                        }
996                } catch (Exception e) {
997                        e.printStackTrace();
998                        System.err.println(e.getMessage());
999                }
1000        }
1001
1002        public static void systemWarn(String clazz, String message, Object obj) {
1003                try {
1004                        if (LEVEL <= LEVEL_WARN) {
1005                                Object[] params = new Object[] { obj + "" };
1006                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
1007                                        String msg = (message + "").replaceAll("\\{\\}", "%s");
1008                                        String _message = String.format(msg, converParams(params));
1009                                        Map<String, Object> map = new HashMap<String, Object>();
1010                                        map.put("message", _message);
1011                                        log("WARN", clazz, map);
1012                                }
1013                                if (LoggerFactory.isLoadedConfig()) {
1014                                        LoggerFactory.getLogger(clazz).warn(message, params);
1015                                }
1016                        }
1017                } catch (Exception e) {
1018                        e.printStackTrace();
1019                        System.err.println(e.getMessage());
1020                }
1021        }
1022
1023        public static void systemWarn(Class clazz, String message, Object... params) {
1024                try {
1025                        if (LEVEL <= LEVEL_WARN) {
1026                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
1027                                        String msg = (message + "").replaceAll("\\{\\}", "%s");
1028                                        String _message = String.format(msg, converParams(params));
1029                                        Map<String, Object> map = new HashMap<String, Object>();
1030                                        map.put("message", _message);
1031                                        log("WARN", clazz, map);
1032                                }
1033                                if (LoggerFactory.isLoadedConfig()) {
1034                                        LoggerFactory.getLogger(clazz).warn(message, params);
1035                                }
1036                        }
1037                } catch (Exception e) {
1038                        e.printStackTrace();
1039                        System.err.println(e.getMessage());
1040                }
1041        }
1042
1043        public static void systemWarn(Class clazz, String message, Object obj) {
1044                try {
1045                        if (LEVEL <= LEVEL_WARN) {
1046                                Object[] params = new Object[] { obj + "" };
1047                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
1048                                        String msg = (message + "").replaceAll("\\{\\}", "%s");
1049                                        String _message = String.format(msg, converParams(params));
1050                                        Map<String, Object> map = new HashMap<String, Object>();
1051                                        map.put("message", _message);
1052                                        log("WARN", clazz, map);
1053                                }
1054                                if (LoggerFactory.isLoadedConfig()) {
1055                                        LoggerFactory.getLogger(clazz).warn(message, params);
1056                                }
1057                        }
1058                } catch (Exception e) {
1059                        e.printStackTrace();
1060                        System.err.println(e.getMessage());
1061                }
1062        }
1063
1064        public static void systemInfo(String clazz, String message) {
1065                try {
1066                        if (LEVEL <= LEVEL_INFO) {
1067                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
1068                                        Map<String, Object> map = new HashMap<String, Object>();
1069                                        map.put("message", message);
1070                                        log("INFO", clazz, map);
1071                                }
1072                                if (LoggerFactory.isLoadedConfig()) {
1073                                        LoggerFactory.getLogger(clazz).info(message);
1074                                }
1075                        }
1076                } catch (Exception e) {
1077                        e.printStackTrace();
1078                        System.err.println(e.getMessage());
1079                }
1080        }
1081
1082        public static void systemInfo(Class clazz, String message) {
1083                try {
1084                        if (LEVEL <= LEVEL_INFO) {
1085                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
1086                                        Map<String, Object> map = new HashMap<String, Object>();
1087                                        map.put("message", message);
1088                                        log("INFO", clazz, map);
1089                                }
1090                                if (LoggerFactory.isLoadedConfig()) {
1091                                        LoggerFactory.getLogger(clazz).info(message);
1092                                }
1093                        }
1094                } catch (Exception e) {
1095                        e.printStackTrace();
1096                        System.err.println(e.getMessage());
1097                }
1098        }
1099
1100        public static void systemInfo(String clazz, String message, Exception exception) {
1101                try {
1102                        if (LEVEL <= LEVEL_INFO) {
1103                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
1104                                        Map<String, Object> map = new HashMap<String, Object>();
1105                                        map.put("message", message);
1106                                        map.put("exception", exception);
1107                                        log("INFO", clazz, map);
1108                                }
1109                                if (LoggerFactory.isLoadedConfig()) {
1110                                        LoggerFactory.getLogger(clazz).info(message, exception);
1111                                }
1112                        }
1113                } catch (Exception e) {
1114                        e.printStackTrace();
1115                        System.err.println(e.getMessage());
1116                }
1117        }
1118
1119        public static void systemInfo(Class clazz, String message, Exception exception) {
1120                try {
1121                        if (LEVEL <= LEVEL_INFO) {
1122                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
1123                                        Map<String, Object> map = new HashMap<String, Object>();
1124                                        map.put("message", message);
1125                                        map.put("exception", exception);
1126                                        log("INFO", clazz, map);
1127                                }
1128                                if (LoggerFactory.isLoadedConfig()) {
1129                                        LoggerFactory.getLogger(clazz).info(message, exception);
1130                                }
1131                        }
1132                } catch (Exception e) {
1133                        e.printStackTrace();
1134                        System.err.println(e.getMessage());
1135                }
1136        }
1137
1138        public static void systemInfo(String clazz, Exception exception) {
1139                try {
1140                        if (LEVEL <= LEVEL_INFO) {
1141                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
1142                                        Map<String, Object> map = new HashMap<String, Object>();
1143                                        map.put("message", exception.getMessage());
1144                                        map.put("exception", exception);
1145                                        log("INFO", clazz, map);
1146                                }
1147                                if (LoggerFactory.isLoadedConfig()) {
1148                                        LoggerFactory.getLogger(clazz).info(exception);
1149                                }
1150                        }
1151                } catch (Exception e) {
1152                        e.printStackTrace();
1153                        System.err.println(e.getMessage());
1154                }
1155        }
1156
1157        public static void systemInfo(Class clazz, Exception exception) {
1158                try {
1159                        if (LEVEL <= LEVEL_INFO) {
1160                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
1161                                        Map<String, Object> map = new HashMap<String, Object>();
1162                                        map.put("message", exception.getMessage());
1163                                        map.put("exception", exception);
1164                                        log("INFO", clazz, map);
1165                                }
1166                                if (LoggerFactory.isLoadedConfig()) {
1167                                        LoggerFactory.getLogger(clazz).info(exception);
1168                                }
1169                        }
1170                } catch (Exception e) {
1171                        e.printStackTrace();
1172                        System.err.println(e.getMessage());
1173                }
1174        }
1175
1176        public static void systemInfo(String clazz, String message, Object... params) {
1177                try {
1178                        if (LEVEL <= LEVEL_INFO) {
1179                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
1180                                        String msg = (message + "").replaceAll("\\{\\}", "%s");
1181                                        String _message = String.format(msg, converParams(params));
1182                                        Map<String, Object> map = new HashMap<String, Object>();
1183                                        map.put("message", _message);
1184                                        log("INFO", clazz, map);
1185                                }
1186                                if (LoggerFactory.isLoadedConfig()) {
1187                                        LoggerFactory.getLogger(clazz).info(message, params);
1188                                }
1189                        }
1190                } catch (Exception e) {
1191                        e.printStackTrace();
1192                        System.err.println(e.getMessage());
1193                }
1194        }
1195
1196        public static void systemInfo(String clazz, String message, Object obj) {
1197                try {
1198                        if (LEVEL <= LEVEL_INFO) {
1199                                Object[] params = new Object[] { obj + "" };
1200                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
1201                                        String msg = (message + "").replaceAll("\\{\\}", "%s");
1202                                        String _message = String.format(msg, converParams(params));
1203                                        Map<String, Object> map = new HashMap<String, Object>();
1204                                        map.put("message", _message);
1205                                        log("INFO", clazz, map);
1206                                }
1207                                if (LoggerFactory.isLoadedConfig()) {
1208                                        LoggerFactory.getLogger(clazz).info(message, params);
1209                                }
1210                        }
1211                } catch (Exception e) {
1212                        e.printStackTrace();
1213                        System.err.println(e.getMessage());
1214                }
1215        }
1216
1217        public static void systemInfo(Class clazz, String message, Object... params) {
1218                try {
1219                        if (LEVEL <= LEVEL_INFO) {
1220                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
1221                                        String msg = (message + "").replaceAll("\\{\\}", "%s");
1222                                        String _message = String.format(msg, converParams(params));
1223                                        Map<String, Object> map = new HashMap<String, Object>();
1224                                        map.put("message", _message);
1225                                        log("INFO", clazz, map);
1226                                }
1227                                if (LoggerFactory.isLoadedConfig()) {
1228                                        LoggerFactory.getLogger(clazz).info(message, params);
1229                                }
1230                        }
1231                } catch (Exception e) {
1232                        e.printStackTrace();
1233                        System.err.println(e.getMessage());
1234                }
1235        }
1236
1237        public static void systemInfo(Class clazz, String message, Object obj) {
1238                try {
1239                        if (LEVEL <= LEVEL_INFO) {
1240                                Object[] params = new Object[] { obj + "" };
1241                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
1242                                        String msg = (message + "").replaceAll("\\{\\}", "%s");
1243                                        String _message = String.format(msg, converParams(params));
1244                                        Map<String, Object> map = new HashMap<String, Object>();
1245                                        map.put("message", _message);
1246                                        log("INFO", clazz, map);
1247                                }
1248                                if (LoggerFactory.isLoadedConfig()) {
1249                                        LoggerFactory.getLogger(clazz).info(message, params);
1250                                }
1251                        }
1252                } catch (Exception e) {
1253                        e.printStackTrace();
1254                        System.err.println(e.getMessage());
1255                }
1256        }
1257
1258        public static void systemDebug(String clazz, String message) {
1259                try {
1260                        if (LEVEL <= LEVEL_DEBUG) {
1261                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
1262                                        Map<String, Object> map = new HashMap<String, Object>();
1263                                        map.put("message", message);
1264                                        log("DEBUG", clazz, map);
1265                                }
1266                                if (LoggerFactory.isLoadedConfig()) {
1267                                        LoggerFactory.getLogger(clazz).debug(message);
1268                                }
1269                        }
1270                } catch (Exception e) {
1271                        e.printStackTrace();
1272                        System.err.println(e.getMessage());
1273                }
1274        }
1275
1276        public static void systemDebug(Class clazz, String message) {
1277                try {
1278                        if (LEVEL <= LEVEL_DEBUG) {
1279                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
1280                                        Map<String, Object> map = new HashMap<String, Object>();
1281                                        map.put("message", message);
1282                                        log("DEBUG", clazz, map);
1283                                }
1284                                if (LoggerFactory.isLoadedConfig()) {
1285                                        LoggerFactory.getLogger(clazz).debug(message);
1286                                }
1287                        }
1288                } catch (Exception e) {
1289                        e.printStackTrace();
1290                        System.err.println(e.getMessage());
1291                }
1292        }
1293
1294        public static void systemDebug(String clazz, String message, Exception exception) {
1295                try {
1296                        if (LEVEL <= LEVEL_DEBUG) {
1297                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
1298                                        Map<String, Object> map = new HashMap<String, Object>();
1299                                        map.put("message", message);
1300                                        map.put("exception", exception);
1301                                        log("DEBUG", clazz, map);
1302                                }
1303                                if (LoggerFactory.isLoadedConfig()) {
1304                                        LoggerFactory.getLogger(clazz).debug(exception);
1305                                }
1306                        }
1307                } catch (Exception e) {
1308                        e.printStackTrace();
1309                        System.err.println(e.getMessage());
1310                }
1311        }
1312
1313        public static void systemDebug(Class clazz, String message, Exception exception) {
1314                try {
1315                        if (LEVEL <= LEVEL_DEBUG) {
1316                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
1317                                        Map<String, Object> map = new HashMap<String, Object>();
1318                                        map.put("message", message);
1319                                        map.put("exception", exception);
1320                                        log("DEBUG", clazz, map);
1321                                }
1322                                if (LoggerFactory.isLoadedConfig()) {
1323                                        LoggerFactory.getLogger(clazz).debug(exception);
1324                                }
1325                        }
1326                } catch (Exception e) {
1327                        e.printStackTrace();
1328                        System.err.println(e.getMessage());
1329                }
1330        }
1331
1332        public static void systemDebug(String clazz, Exception exception) {
1333                try {
1334                        if (LEVEL <= LEVEL_DEBUG) {
1335                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
1336                                        Map<String, Object> map = new HashMap<String, Object>();
1337                                        map.put("message", exception.getMessage());
1338                                        map.put("exception", exception);
1339                                        log("DEBUG", clazz, map);
1340                                }
1341                                if (LoggerFactory.isLoadedConfig()) {
1342                                        LoggerFactory.getLogger(clazz).debug(exception);
1343                                }
1344                        }
1345                } catch (Exception e) {
1346                        e.printStackTrace();
1347                        System.err.println(e.getMessage());
1348                }
1349        }
1350
1351        public static void systemDebug(Class clazz, Exception exception) {
1352                try {
1353                        if (LEVEL <= LEVEL_DEBUG) {
1354                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
1355                                        Map<String, Object> map = new HashMap<String, Object>();
1356                                        map.put("message", exception.getMessage());
1357                                        map.put("exception", exception);
1358                                        log("DEBUG", clazz, map);
1359                                }
1360                                if (LoggerFactory.isLoadedConfig()) {
1361                                        LoggerFactory.getLogger(clazz).debug(exception);
1362                                }
1363                        }
1364                } catch (Exception e) {
1365                        e.printStackTrace();
1366                        System.err.println(e.getMessage());
1367                }
1368        }
1369
1370        public static void systemDebug(String clazz, String message, Object... params) {
1371                try {
1372                        if (LEVEL <= LEVEL_DEBUG) {
1373                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
1374                                        String msg = (message + "").replaceAll("\\{\\}", "%s");
1375                                        String _message = String.format(msg, converParams(params));
1376                                        Map<String, Object> map = new HashMap<String, Object>();
1377                                        map.put("message", _message);
1378                                        log("DEBUG", clazz, map);
1379                                }
1380                                if (LoggerFactory.isLoadedConfig()) {
1381                                        LoggerFactory.getLogger(clazz).debug(message, params);
1382                                }
1383                        }
1384                } catch (Exception e) {
1385                        e.printStackTrace();
1386                        System.err.println(e.getMessage());
1387                }
1388        }
1389
1390        public static void systemDebug(String clazz, String message, Object obj) {
1391                try {
1392                        if (LEVEL <= LEVEL_DEBUG) {
1393                                Object[] params = new Object[] { obj + "" };
1394                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
1395                                        String msg = (message + "").replaceAll("\\{\\}", "%s");
1396                                        String _message = String.format(msg, converParams(params));
1397                                        Map<String, Object> map = new HashMap<String, Object>();
1398                                        map.put("message", _message);
1399                                        log("DEBUG", clazz, map);
1400                                }
1401                                if (LoggerFactory.isLoadedConfig()) {
1402                                        LoggerFactory.getLogger(clazz).debug(message, params);
1403                                }
1404                        }
1405                } catch (Exception e) {
1406                        e.printStackTrace();
1407                        System.err.println(e.getMessage());
1408                }
1409        }
1410
1411        public static void systemDebug(Class clazz, String message, Object... params) {
1412                try {
1413                        if (LEVEL <= LEVEL_DEBUG) {
1414                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
1415                                        String msg = (message + "").replaceAll("\\{\\}", "%s");
1416                                        String _message = String.format(msg, converParams(params));
1417                                        Map<String, Object> map = new HashMap<String, Object>();
1418                                        map.put("message", _message);
1419                                        log("DEBUG", clazz, map);
1420                                }
1421                                if (LoggerFactory.isLoadedConfig()) {
1422                                        LoggerFactory.getLogger(clazz).debug(message, params);
1423                                }
1424                        }
1425                } catch (Exception e) {
1426                        e.printStackTrace();
1427                        System.err.println(e.getMessage());
1428                }
1429        }
1430
1431        public static void systemDebug(Class clazz, String message, Object obj) {
1432                try {
1433                        if (LEVEL <= LEVEL_DEBUG) {
1434                                Object[] params = new Object[] { obj + "" };
1435                                if (LogRecord.isSystemOut() || !LoggerFactory.isLoadedConfig()) {
1436                                        String msg = (message + "").replaceAll("\\{\\}", "%s");
1437                                        String _message = String.format(msg, converParams(params));
1438                                        Map<String, Object> map = new HashMap<String, Object>();
1439                                        map.put("message", _message);
1440                                        log("DEBUG", clazz, map);
1441                                }
1442                                if (LoggerFactory.isLoadedConfig()) {
1443                                        LoggerFactory.getLogger(clazz).debug(message, params);
1444                                }
1445                        }
1446                } catch (Exception e) {
1447                        e.printStackTrace();
1448                        System.err.println(e.getMessage());
1449                }
1450        }
1451
1452        private static void log(String level, String clazzName, Map<String, Object> map) {
1453                map.put("level", level);
1454
1455                StackTraceElement[] threadStacks = Thread.currentThread().getStackTrace();
1456                for (StackTraceElement stack : threadStacks) {
1457                        String className = stack.getClassName();
1458                        if (className.equals(clazzName)) {
1459                                String fileName = stack.getFileName();
1460                                String methodName = stack.getMethodName();
1461                                int lineNumber = stack.getLineNumber();
1462                                map.put("log_file_name", fileName);
1463                                map.put("log_class_name", className);
1464                                map.put("log_method_name", methodName);
1465                                map.put("log_line_number", lineNumber);
1466                                break;
1467                        }
1468                }
1469
1470                Thread currentThread = Thread.currentThread();
1471                long threadId = currentThread.getId();
1472                String threadName = currentThread.getName();
1473
1474                Date logTime = new Date();
1475                map.put("log_time", logTime);
1476                map.put("log_time_format", LogRecord.getLogTimeFormat().format(logTime));
1477                map.put("log_thread_id", threadId);
1478                map.put("log_thread_name", threadName);
1479                map.put("hostname", CommonTools.getHostname());
1480
1481                Exception exception = (Exception) map.get("exception");
1482                if (exception != null) {
1483                        StringWriter sw = new StringWriter();
1484                        exception.printStackTrace(new PrintWriter(sw));
1485                        String exceptionAsString = sw.toString();
1486                        String exceptionClassName = exception.getClass().getName();
1487                        String exceptionMessage = exception.getMessage();
1488                        StackTraceElement[] stacks = exception.getStackTrace();
1489                        String fileName = null;
1490                        String className = null;
1491                        String methodName = null;
1492                        int lineNumber = 0;
1493                        for (StackTraceElement stack : stacks) {
1494                                fileName = stack.getFileName();
1495                                className = stack.getClassName();
1496                                methodName = stack.getMethodName();
1497                                lineNumber = stack.getLineNumber();
1498                                break;
1499                        }
1500                        map.put("stack_trace", sw.toString());
1501                        map.put("exception_class_name", exceptionClassName);
1502                        map.put("exception_message", exceptionMessage);
1503                        map.put("issued_file_name", fileName);
1504                        map.put("issued_class_name", className);
1505                        map.put("issued_method_name", methodName);
1506                        map.put("issued_line_number", lineNumber);
1507                }
1508
1509                if (!map.containsKey("stack_trace"))
1510                        map.put("stack_trace", "");
1511
1512                if (!map.containsKey("stack_trace_single"))
1513                        map.put("stack_trace_single", "");
1514
1515                if (!map.containsKey("log_file_name"))
1516                        map.put("log_file_name", "Unknown");
1517
1518                if (!map.containsKey("log_class_name"))
1519                        map.put("log_class_name", clazzName);
1520
1521                if (!map.containsKey("log_method_name"))
1522                        map.put("log_method_name", "Unknown");
1523
1524                if (!map.containsKey("log_line_number"))
1525                        map.put("log_line_number", 0);
1526
1527                map.remove("exception");
1528
1529                String messageFormat = DEFAULT_MESSAGE_FORMAT;
1530                if (LoggerFactory.getConfigProperties() != null) {
1531                        messageFormat = LoggerFactory.getConfigProperties().getString("MessageFormat", DEFAULT_MESSAGE_FORMAT);
1532                }
1533                String msg = LogRecord.format(messageFormat, map);
1534                System.out.println(msg);
1535        }
1536
1537        private static void log(String level, Class clazz, Map<String, Object> map) {
1538                log(level, clazz.getName(), map);
1539        }
1540
1541        protected void log(Integer _level, Map<String, Object> map) {
1542                String levelStr = null;
1543                String log4jMethodName = null;
1544                switch (_level) {
1545                case 0:
1546                        levelStr = "DEBUG";
1547                        break;
1548                case 1:
1549                        levelStr = "INFO";
1550                        break;
1551                case 2:
1552                        levelStr = "WARN";
1553                        break;
1554                case 3:
1555                        levelStr = "ERROR";
1556                        break;
1557                case 4:
1558                        levelStr = "MARK";
1559                        break;
1560                default:
1561                        break;
1562                }
1563                map.put("level", levelStr);
1564
1565                StackTraceElement[] threadStacks = Thread.currentThread().getStackTrace();
1566                for (StackTraceElement stack : threadStacks) {
1567                        String className = stack.getClassName();
1568                        if (className.equals(clazzName)) {
1569                                String fileName = stack.getFileName();
1570                                String methodName = stack.getMethodName();
1571                                int lineNumber = stack.getLineNumber();
1572                                map.put("log_file_name", fileName);
1573                                map.put("log_class_name", className);
1574                                map.put("log_method_name", methodName);
1575                                map.put("log_line_number", lineNumber);
1576                                break;
1577                        }
1578                }
1579
1580                Thread currentThread = Thread.currentThread();
1581                long threadId = currentThread.getId();
1582                String threadName = currentThread.getName();
1583
1584                Date logTime = new Date();
1585                map.put("log_time", logTime);
1586                map.put("log_time_format", LogRecord.getLogTimeFormat().format(logTime));
1587                map.put("log_thread_id", threadId);
1588                map.put("log_thread_name", threadName);
1589
1590                Exception exception = (Exception) map.get("exception");
1591                if (exception != null) {
1592                        StringWriter sw = new StringWriter();
1593                        exception.printStackTrace(new PrintWriter(sw));
1594                        String exceptionAsString = sw.toString();
1595                        String exceptionClassName = exception.getClass().getName();
1596                        String exceptionMessage = exception.getMessage();
1597                        StackTraceElement[] stacks = exception.getStackTrace();
1598                        String fileName = null;
1599                        String className = null;
1600                        String methodName = null;
1601                        int lineNumber = 0;
1602                        for (StackTraceElement stack : stacks) {
1603                                fileName = stack.getFileName();
1604                                className = stack.getClassName();
1605                                methodName = stack.getMethodName();
1606                                lineNumber = stack.getLineNumber();
1607                                break;
1608                        }
1609                        map.put("stack_trace", sw.toString());
1610                        map.put("stack_trace_single", toSingle(sw.toString()));
1611                        map.put("exception_class_name", exceptionClassName);
1612                        map.put("exception_message", exceptionMessage);
1613                        map.put("exception_message_single", toSingle(exceptionMessage));
1614                        map.put("issued_file_name", fileName);
1615                        map.put("issued_class_name", className);
1616                        map.put("issued_method_name", methodName);
1617                        map.put("issued_line_number", lineNumber);
1618                }
1619
1620                if (map.containsKey("message")) {
1621                        map.put("message_single", toSingle((String) map.get("message")));
1622                }
1623
1624                if (!map.containsKey("stack_trace"))
1625                        map.put("stack_trace", "");
1626
1627                if (!map.containsKey("stack_trace_single"))
1628                        map.put("stack_trace_single", "");
1629
1630                if (!map.containsKey("log_file_name"))
1631                        map.put("log_file_name", "Unknown");
1632
1633                if (!map.containsKey("log_class_name"))
1634                        map.put("log_class_name", clazzName);
1635
1636                if (!map.containsKey("log_method_name"))
1637                        map.put("log_method_name", "Unknown");
1638
1639                if (!map.containsKey("log_line_number"))
1640                        map.put("log_line_number", 0);
1641
1642                map.put("id", CommonTools.generateId(16));
1643                map.put("created_at", CommonTools.generateId(16));
1644                map.put("hostname", hostname);
1645
1646                map.remove("exception");
1647
1648                String logClassName = String.format("%s.%s", map.get("log_class_name"), map.get("log_method_name"));
1649                boolean passEnableRegex = checkClassNameEnableRegex(logClassName);
1650                boolean passDisableRegex = !checkClassNameDisableRegex(logClassName);
1651
1652                if (passEnableRegex && passDisableRegex) {
1653                        LogRecord.add(map);
1654                }
1655        }
1656
1657        private boolean checkClassNameEnableRegex(String logClassName) {
1658                if (!ConfigProperties.isBlank(classNameEnableRegex)) {
1659                        return logClassName.matches(classNameEnableRegex);
1660                }
1661                return true;
1662        }
1663
1664        private boolean checkClassNameDisableRegex(String logClassName) {
1665                if (!ConfigProperties.isBlank(classNameDisableRegex)) {
1666                        return logClassName.matches(classNameDisableRegex);
1667                }
1668                return false;
1669        }
1670
1671        public String getClazzName() {
1672                return clazzName;
1673        }
1674
1675        private String toSingle(String text) {
1676                return CodeEscape.escapeToSingleLineForCsv(text);
1677        }
1678
1679        private static String[] converParams(Object... params) {
1680                if (params == null)
1681                        return new String[] { "null" };
1682
1683                String[] stringParams = new String[params.length];
1684                for (int i = 0; i < params.length; i++) {
1685                        stringParams[i] = params[i] + "";
1686                }
1687                return stringParams;
1688        }
1689
1690}