Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My solution, which seems to work.</p> <p>Write a custom class that extends the Log4j system</p> <p>public class myLogger extends Logger { ... stuff ...</p> <p>}</p> <p>for the 'stuff' you need to implement the following (or at least I did)</p> <p>A constructor of some sort public myLogger (String name)</p> <p>.. in each class, where you want / need to include a static member of the myLogger</p> <pre><code>private static final myLogger errorLog = new myLogger("nameOfClassOrWhateverNameYouFancy") </code></pre> <p>If you use the same name across all your classes they will log to the same logging instance (this you probably already know)</p> <p>back in your myLogger class you need to implement a method for setting the log level (this is how I did it...)</p> <pre><code>/** * * The add(int i, String s) method * @param s the message string * * @param i used in the switch to create the required level of debug * @param s the message to send * * use this method to create a log message of any required level * The levels are as follows: * 1 debug - used during production * 2 info - may be used for messages to user * 3 warn - warnings for requesting users re-enter information (data type checking etc) * 4 error - programmatic / user error may result in unexpected output * 5 fatal - severe error (comms error) the system will terminate, cleanly if possible, current state will be written to file * * Prior to all this however the method determines the calling class of itself * */ public void add(int i, String s) { switch (i) { case 1: log.debug(s);break; case 2: log.info(s);break; case 3: log.warn(s);break; case 4: log.error(s);break; case 5: log.fatal(s);break; } } </code></pre> <p>Now if you would like some specific loging instances for 'strange stuff' you have 2 options, as I said previously, you can create a static member to the named logger, or you can add static members to your myLogger class (which is how I did it).</p> <p>Then for each of these static members you probably want a unique </p> <pre><code>set[nameOfLogger](int i, string s) </code></pre> <p>which basically has the same sort of structure as the method above.</p> <p>Now if you want to call it from a class you call</p> <pre><code>myLogger.set[nameOfLogger](1, "s") </code></pre> <p>and you are away.</p> <p>Then you need to add the information about the log level (DEBUG, WARN etc) and where to send the messages to (console, file etc) into your log4j.properties (as I use) or into the log4j.xml.</p> <p>Remember to ensure that the lines about setting the static named loggers occur after the instance of the rootLogger, that caught me out for a long while!</p> <p>I've included what I feel are fairly extensive instruction as I fought this for quite some time and only got it to finaly work today!</p> <p>I am a happy bunny now.... untill I break something else ;)</p> <p>David</p> <p>Sorry just noticed you didn't want to use a log4j.properties file. any reason in particular?</p> <p>If not just</p> <pre><code>import org.apache.log4j.Logger; </code></pre> <p>create a logging instance</p> <pre><code>private Logger jUnitTestingLogger; </code></pre> <p>then set the output using the various method calls (i'm particularly thinking of the add appender), I've never done it like this so wouldn't know how!</p> <p>Interestingly there is an assertLog(boolean assertion, string message) method that may be good for you in your instance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload