View Javadoc
1   /*
2    * Oceanus: Java Utilities
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  package io.github.tonywasher.joceanus.oceanus.logger;
18  
19  /**
20   * Logger instance.
21   */
22  public class OceanusLogger {
23      /**
24       * The log manager instance.
25       */
26      private final OceanusLogManager theManager;
27  
28      /**
29       * The class for which this is the logger.
30       */
31      private final Class<?> theOwner;
32  
33      /**
34       * Constructor.
35       *
36       * @param pManager the manager
37       * @param pOwner   the owning class
38       */
39      public OceanusLogger(final OceanusLogManager pManager,
40                           final Class<?> pOwner) {
41          theManager = pManager;
42          theOwner = pOwner;
43      }
44  
45      /**
46       * Write a debug message with parameters.
47       *
48       * @param pFormat the format
49       * @param pArgs   the arguments
50       */
51      public void debug(final String pFormat,
52                        final Object... pArgs) {
53          final String myMessage = String.format(pFormat, pArgs);
54          final String myLogMessage = theManager.formatMessage(theOwner, OceanusLogLevel.DEBUG, myMessage);
55          theManager.writeLogMessage(myLogMessage);
56      }
57  
58      /**
59       * Write a debug message with hex data.
60       *
61       * @param pMessage the message
62       * @param pData    the data
63       */
64      public void debug(final String pMessage,
65                        final byte[] pData) {
66          final String myLogMessage = theManager.formatMessage(theOwner, OceanusLogLevel.DEBUG, pMessage);
67          final String myLogData = OceanusLogManager.formatData(pData);
68          theManager.writeLogMessage(myLogMessage + myLogData);
69      }
70  
71      /**
72       * Write a debug message with hex data.
73       *
74       * @param pMessage the message
75       * @param pData    the data
76       * @param pOffset  the offset
77       * @param pLength  the length of data
78       */
79      public void debug(final String pMessage,
80                        final byte[] pData,
81                        final int pOffset,
82                        final int pLength) {
83          final String myLogMessage = theManager.formatMessage(theOwner, OceanusLogLevel.DEBUG, pMessage);
84          final String myLogData = OceanusLogManager.formatData(pData, pOffset, pLength);
85          theManager.writeLogMessage(myLogMessage + myLogData);
86      }
87  
88      /**
89       * Write an information message with parameters.
90       *
91       * @param pFormat the format
92       * @param pArgs   the arguments
93       */
94      public void info(final String pFormat,
95                       final Object... pArgs) {
96          final String myMessage = String.format(pFormat, pArgs);
97          final String myLogMessage = theManager.formatMessage(theOwner, OceanusLogLevel.INFO, myMessage);
98          theManager.writeLogMessage(myLogMessage);
99      }
100 
101     /**
102      * Write an error message with parameters.
103      *
104      * @param pFormat the format
105      * @param pArgs   the arguments
106      */
107     public void error(final String pFormat,
108                       final Object... pArgs) {
109         final String myMessage = String.format(pFormat, pArgs);
110         final String myLogMessage = theManager.formatMessage(theOwner, OceanusLogLevel.ERROR, myMessage);
111         theManager.writeLogMessage(myLogMessage);
112     }
113 
114     /**
115      * Write an error message with exception.
116      *
117      * @param pMessage   the message
118      * @param pException the exception
119      */
120     public void error(final String pMessage,
121                       final Throwable pException) {
122         final String myLogMessage = theManager.formatMessage(theOwner, OceanusLogLevel.ERROR, pMessage);
123         theManager.writeLogMessage(myLogMessage, pException);
124     }
125 
126     /**
127      * Write a fatal error message with parameters.
128      *
129      * @param pFormat the format
130      * @param pArgs   the arguments
131      */
132     public void fatal(final String pFormat,
133                       final Object... pArgs) {
134         final String myMessage = String.format(pFormat, pArgs);
135         final String myLogMessage = theManager.formatMessage(theOwner, OceanusLogLevel.FATAL, myMessage);
136         theManager.writeLogMessage(myLogMessage);
137     }
138 
139     /**
140      * Write a fatal error message with exception.
141      *
142      * @param pMessage   the message
143      * @param pException the exception
144      */
145     public void fatal(final String pMessage,
146                       final Throwable pException) {
147         final String myLogMessage = theManager.formatMessage(theOwner, OceanusLogLevel.FATAL, pMessage);
148         theManager.writeLogMessage(myLogMessage, pException);
149     }
150 }