How does one intercept System.out.println stream.

If you want some functionality wherein when you call System.out.println - the stream gets written to System.out AND additionally does something else (e.g. send mail).
AND
You do not want to write a new class/method to do so and replace all System.out.println in your source base with this new class/method;
Then how would you be able to achieve this. Thanks in advance.

Here is the basic code, you may need to tune performance. I havent compiler or tested it, but it should give you a good idea.
public class OutputStreamCollection extends OutputStream{
private OutputStream[] outs = new OutputStream[0];
public synchronized void addOutputStream(OutputStream argOut) {
OutputStream[] old = outs;
outs = new OuputStream[old.length + 1];
System.arraycopy(old,0,outs,0, old.length);
outs[outs.length - 1] = argOut;
public syncrhonized void write(int data) {
for(int i = 0; i < outs.length; i++) {
outs.write();
public synchronized void write(byte[] data, int offset, int length) {
for(int i = 0; i < outs.length; i++) {
outs[i].write(data, offset, length);
nor the following code, will print and log to respective files whatever is going to stdout or stderr
OutpputStreamCollection out = new OutputStreamCollection();
out.addOutputStream(System.out);
out.addOutputStream(new FileOutputStream("stdout.log"));
System.setOut(out);
OutpputStreamCollection err = new OutputStreamCollection();
out.addOutputStream(System.err);
out.addOutputStream(new FileOutputStream("stderr.log"));
System.setOut(err);
you can write adapter classes to process the stream in any way you need, like logging with log4J, or sending mail depending on some crieteria.
Hope it helps.

Similar Messages

  • How do you change system.out.println colour?

    Hi, I am developing some basic rmi client/server applications, I have an 2D array on a central server being read/written by remote clients. all the development is using system.out. statements at the moment (hopefully being replaced by GUI later) my question is when a client remote calls a read operation, and reads an array location, eg x3, y4 I want to print the location contents in a different colour to indicate the read, at the moment the displayArray() simply has nested for loops to cycle through the array and System.out.println statements to get it to the screen. I wonder is there a simple way to change the colour of system.out type statements? thanks for any help

    I could be wrong, but I don't think that is possible. The System.out and other streams are just streams of characters, and doesn't contain any data about the characters. You could possibly get it to work if you knew which terminal the stream was going to and if the terminal had escape commands that could set colors. BBS browsers have that functionality, but I think that would be a hard path to follow.
    If you are printing out a lot of data and need some color to aid it's readability, try printing directly to your graphic context:
    Graphics g = getGraphics();
    g.setColor(Color.white);
    g.drawString("Hello",100,100);

  • How to :Logging or System.out.println????

    Hi,
    Iam not able to see any System.out.println output in SAP? where exactly can i see it? any configuration required to enable logging? if so how?? please help

    Hi Sujesh,
    see https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/events/webinars/using logging and tracing on the sap web as java.pdf pages 12-17 and pages 22-24.
    Also see http://help.sap.com/saphelp_nw04/helpdata/en/e2/75a74046033913e10000000a155106/frameset.htm for portal specific logging issues.
    Hope it helps
    Detlev

  • HT4112 how does one navigate with out the use of a mouse? do we still need to use finger to move to different things?

    how does one navigate the keyboard without a mouse. Do we still use finger to move from place to place?

    Yes you still need to use your finger to do all of the things that you need to use it for even if you use a wireless keyboard. You are merely adding a larger more robust keyboard to the device. You are not replacing the touch gestures features with the use of an external keyboard.

  • Difference between System.out.println(..)and PrintWriter.println(..)

    Hello every one
    With System.out.println(..) we can print to Console.
    But System.out internally creates PrintWriter object.So why cant we use
    PrintWriter.println(..) directly to write to console.
    Is there aany disadvvantage with PrintWriter.println(..).
    If so can anyone please tell me what is that and
    what is the advantage we r going to get with System.out.println(..) method of writing to console.

    kirn291 wrote:
    With System.out.println(..) we can print to Console.If you want to be very precise, then you have to say that System.out.println() writes to the default output stream. This is usually shown at the console, but can equally well go into a file (if you've redirected it) or nowhere (when running without a console).
    But System.out internally creates PrintWriter object.Not exactly. It refers to a PrintStream object.
    So why cant we use
    PrintWriter.println(..) directly to write to console.Because if you create a PrintWriter it is just another PrintWriter that writes to whatever you specified at the constructor. It is not the same thing as the one PrintStream that is used in System.out.
    what is the advantage we r going to get with System.out.println(..) method of writing to console.The advantage is that it's the only way to do it. Could you post an example of how you'd think of a different way to write to the console?

  • System.out.println capture to buffer

    Hi All,
    My application runs other application in back. How can I capture system.out.println() written by another application & display via dialog box.
    Like :execl() or execv() function in C runtime library. Where every printf() message printed to passed buffer.
    Thank you,
    Avin Patel

    If i understand your question correctly, you should execute command for you help.html file. The system will run default browser.
    such as:
    void runBrowser(String file)
    {String cmd = null;   
         Process p;   
              try  {       
                   String os = System.getProperty("os.name");            
                   if (os != null && os.startsWith("Windows")) {           
                        if (os.startsWith("Windows 9") || os.startsWith("Windows Me")) 
                        // Windows 9x/Me               
                             cmd = "start " + file;
    else
                        // Windows NT/2000/XP               
                             cmd = "cmd /c start " + file;
    p = Runtime.getRuntime().exec(cmd);            
    catch(IOException x)
    // couldn't exec browser
    System.err.println("Could not invoke browser, command=" + cmd);
    }

  • Turn off System.out.println

    The nohup.out file is getting too big, how to turn off System.out.println without change any coding?
              

    java weblogic.Server > server.out 2>&1
              cheers
              mbg
              "Jen " <[email protected]> wrote in message
              news:[email protected]..
              >
              > How can I capture the System.out on windows?Thanks
              >
              > "Deyan D. Bektchiev" <[email protected]> wrote:
              > >just put the output to /dev/null on Unix/Linux or "nul" on Windows...
              > >
              > >Of course then the output is lost forever and you'll only have the
              > >Weblogic log files to look at any potential problems.
              > >
              > >--dejan
              > >
              > >java guy wrote:
              > >
              > >>The nohup.out file is getting too big, how to turn off
              System.out.println
              > >without change any coding?
              > >>
              > >>
              > >
              >
              

  • How does System.out.println(). Work inside src.zip file !

    Hello guyz,
    I was just wondering how System.out.println() worked so i opened up the src.zip file and checked the source code. But could not understand it. As written i remember
    "out is an object encapsulated in the System class."
    Thats ok. But i could not understand the code.
        public final static PrintStream out = nullPrintStream();
        private static PrintStream nullPrintStream() throws NullPointerException {
         if (currentTimeMillis() > 0) {
             return null;
         throw new NullPointerException();
        }Also when i ran the DJ Decompiler it decompiled it to this:
    public class One
         public static void main(String args[])
              System.out.println("hello world");
    import java.io.PrintStream;
    public class One
        public One()
        public static void main(String args[])
            System.out.println("hello world");
    }Also, why does it need to import PrintStream ?

    Peter__Lawrey wrote:
    I was just wondering how System.out.println() worked so i opened up the src.zip file and checked the source code.This value is a place holder. This value is changed as soon as enough of the JVM is initialised for printing to work.
    Also, why does it need to import PrintStream ? It doesn't, but it is used in the code so DJ is including it just in case.Sorry,
    But i don't understand at all.

  • Urgent: Redirecting System.out.println to more than one place

    Hi All,
    I want to divert all my System.out.println() statements to a Log file as well as on to the screen.
    But only one of the 2 options actually works.
    So if anyone can suggest me some ways I will appreciate very much.
    My code looks something like this:-
    if (m_filename != null) {
    try {
    FileOutputStream fos = new FileOutputStream(m_filename);
    PrintStream ps = new PrintStream(fos);
    System.setOut(ps);
    System.setErr(ps);
    //This code Added for Printing Onto Screen But it does not work
    Stream ps1 = new PrintStream(System.out);
    System.setOut(ps1);
    System.setErr(ps1);
    } catch (IOException ioe) {
    System.out.println("Could not create log file " + m_filename + " because " + ioe);
    As far as logging onto file is menat it works when i set it but printing onto screen does not.
    How can i achive it
    Thanks
    Raj

    Hi,
    You are right but in my current scenario i cannot incorporate this log4j because my application is using some other framework and I am just trying to override this frameworks loggig behaviour so that it logs to both a log file as well as on to the screen.
    Here is the code that this framework uses for logging and i am simply overriding it to do the reqd thing but its not working for me:-
    * Resets the System's out and err OutputStreams to the Launcher's
    * own ThreadedOutputStream, so that output from different threads
    * can be tracked efficiently.
    protected void setOutputStreams() {
         if (scInfo.size() <= 1) {
    // don't need Threaded handling with one thread
    if (m_filename != null) {
    try {
    FileOutputStream fos = new FileOutputStream(m_filename);
    PrintStream ps = new PrintStream(fos);
    System.setOut(ps);
    System.setErr(ps);
    //Added for Printing Onto Screen
                             PrintStream ps1 = new PrintStream(System.out);
    System.setOut(ps1);
    System.setErr(ps1);
    } catch (IOException ioe) {
    Debug.fatalError("Launcher.setOutputStreams","Could not create log file " + m_filename + " because " + ioe);
    else {
    Debug.information("Launcher.setOutputStreams","Initializing ThreadedPrintStream");
    if (m_filename == null) {
    System.setOut(new ThreadedPrintStream(System.out));
    System.setErr(new ThreadedPrintStream(System.err));
    else {
    PrintStream out = new ThreadedPrintStream(m_filename);
    System.setOut(out);
    System.setErr(out);
    //Added for Printing Onto Screen
    System.setOut(new ThreadedPrintStream(System.out));
    System.setErr(new ThreadedPrintStream(System.err));
    Debug.information("Launcher.setOutputStreams","ThreadedPrintStream initialized.");
    Thanks
    Raj

  • Which toString() method does System.out.println() call?

    Which toString() method does System.out.println() call? I know that if I did something like System.out.println( new myClass() ) that the toString being called would be that of the myClass class.
    My initial thought about this was that System.out.println() is the equivalent of System.out.println( new Object() ) but I remember trying that in a test program and it gave me a myClass@some_hash_code type of message when adding the new Object() part within the System.out.println().
    Could someone please tell me which toString the System.out.println() statement calls? Also, it would be great if an elaboration follows :).
    Any input woudl be greatly appreciated!
    Thanks in advance!

    s3a wrote:
    My initial thought about this was that System.out.println() is the equivalent of System.out.println( new Object() ) Huh?
    In one method call you pass no parameters in the other method call you pass one parameter. How could you possibly think that they would do the same thing?
    Could someone please tell me which toString the System.out.println() statement calls? Also, it would be great if an elaboration follows :).Yes. The toString of the class of the object which you pass to the method. If that class does not have a toString method it uses the toString method it inherits from the parent class. If the parent class does not have a toString method it uses the one inherited and so and so on all the way up to the Object class.

  • Intercepting System.exit() and System.out.println() calls

    hi there,
    I have often problems when working with code that uses System.exit() and System.out.println() extensively, because it becomes difficult to debug.
    Basically I do have wrappers for System.exit() (my own static exit function) and for System.out.println() (log4j).
    Still not all programmers are using these methods; Probably the only way to enforce this is some kind of code warrior, but I was hoping to be able to intercept the two System.XXX calls (and throw an appropriate Exception). is this possible ??

    Why not simply make your own security manager andhandle checkExit() and checkWrite?
    Does anyone have a simple example of this? Please?System.exit() can be intercepted using a security manager, but not System.out.xxx.
    Here is a short example:
    //set your security manager
    static{
         SecurityManager s = new OwnSecurityManager();
         System.setSecurityManager(s);
    //redirect the out stream
    try{
         PrintStream ps = new PrintStream(new FileOutputStream("output.txt"));
         System.setOut(ps);
    }catch(IOException ioe){
         ioe.printStackTrace();
    //some tests
    System.out.println("Test");
    System.exit(2);
    //your security manager
    class OwnSecurityManager extends SecurityManager{
         public void checkExit(int code){
           if(code > 0){
             super.checkExit(code);
             throw new SecurityException("Not allowed here->" + code);
         public void checkRead(FileDescriptor fd){
         public void checkRead(String file){
         public void checkRead(String file, Object context){
         public void checkPermission(Permission perm){
           if(perm.getName().equals("exitVM")){
             System.out.println("exitVM");
    }

  • How does one add a personal recorded M4R file to the ring tones in the UK system for iPhone 3GS

    How does one add a personal recorded M4R file to the ring tones in the UK system for iPhone 3GS.
    I have tried all sorts of ways of adding my personal M4R file but can't find the place (within the UK versions of iTunes} to transfer this file so that I can select it as a ring tone.
    I've tried also changing to the US version of iTunes and still can't find a way of doing it, although I have heard that it can be done.

    You should be able to just drop it into the library within iTunes.
    http://cnettv.cnet.com/create-free-iphone-ringtones-using-itunes-8-0/9742-1_53-5 0003920.html
    Check out the video above for a lot good info. The ringtone must be less than 35 seconds.

  • How does one get Time Machine out of a "Preparing to Backup" loop to actually doing a backup? thanks, AR

    How does one get Time Machine out of a "Preparing to Backup" loop to actually doing a backup? thanks, AR

    Hi..
    Thanks for the reply..
    My original I Phone 5 was running IOS 6.12
    My I phone 5S currently runs IOS 8..When I upgraded it, it had IOS 7 on it which made the I Photo app compatible with everything below IOS 8...( which the photo app is not compatible with ) so I just connected the I Phone 5S to my computer and thinking the new phone had IOS 7 on it that these was nothing to worry about..I thought all 900 + pictures in the photo stream would transfer but somewhere in the process, I Tunes decides to just go and install IOS 8 which requires that you run a " Photo Migration App" to transfer photos from IOS 7 & below. The problem was is that I didn't want IOS 8 on my new 5S so I thought I wouldn't have to do that part..
    Anyway the phone emerged with IOS 8 on it and the only photos that remained were the ones that were actually on the phone..The 900 + I had in the stream didn't transfer because the Photos that works on IOS 7 and below isn't compatible with IOS 8..I guess you have to use the photo migration app that comes with IOS 8 & I phone 6..But I didn't ask to upgrade to IOS 8...It just did it on it's own!
    I still have the I Phone 5 Back up that I did just before giving it back to Verizon but if I try to restore the I Phone 5S running IOS 8 those photos in the stream will not go back on the phone because IOS 8 isn't compatible with the I Photos" that ran on IOS 7 and below..
    So I have an I Pad mini that runs IOS 7..should I try and erase it...and put that I Phone 5 backup on it and pray the photos that were in the stream come back on it so I can transfer them back to my computer...then erase the I pad mini again and restore it using it's original backup that I have on the computer and the in the cloud?
    I know this sounds confusing but I'm at a loss as to what to do..This is what I found ...http://support.apple.com/en-us/HT201386
    but it doesn't help me..
    Steve

  • Where does System.out.println go?

    Hi Everyone: I know I've seen this topic before, but I'm still having some trouble. I would like to debug my EJBs, and so I've added some System.out.println statements to them. Where does that go? I've looked at the defaultTrace.trc files in the
    C:\usr\sap\P48\JC00\j2ee\cluster\server0\log
    folder, and haven't found any of the text that I think I am writing. I appreciate any guidance! Ian.

    Hi lan,
    I was facing the same question earlier, and now I think I have figured out one possible answer. Actually, where the System.out.println goes is up to the Server Admin to config. There is a default SYSTEM.OUT log controller ( under location controller side) pre-defined to cater for all System.out.println(). All the System.out.println() output is considered as INFO type log message. However, this default SYSTEM.OUT controller is not assigned with any real log destination, thus, we cannot find the output anywhere.
    If you goes to the log configurator (using Visual Admin), you can locate this SYSTEM.OUT controller , and add in a default (Anonymous) destionation for it . (you need to toggle on the advance edit mode from the top menu , then you can add modify the destination settings of a Controller).
    For Anonymous destionation, the println() output will sure go to the defaultTrace.trc (better view it using Log Viewer, instead of viewing it from the log file ).
    Or , you can define a separate file (e.g. c:\temp\myStd.log ) as the log destination ..
    Last but not the least, you need to set the ForceSingleTraceFile setting from 'YES' to 'NO' , then you can see your "myStd.log".
    To change the ForceSingleTraceFile , go to Visual Admin, J2EE server --> Kernel --> LogManager.
    Hope you find the above useful.

  • System.out.println does not print out anything

    I'm wonder why the dos mode does not print out the System.out.println.
    My code has:
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class test extends HttpServlet {
       public void doGet(HttpServletRequest request,
       HttpServletResponse response) throws IOException,
       ServletException {
          System.out.println("Hello");
    The compilation is ok... but it does not display anything... how come?

    "System.out.print()" prints to the log file of the tomcat server. in order to print on the screen use JspWriter.
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class test extends HttpServlet {
    public void doGet(HttpServletRequest request,
    HttpServletResponse response) throws IOException,
    ServletException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("Hello");
    //System.out.println("Hello");
    Hope this helps!

Maybe you are looking for

  • Wifi problems on Macbook since installing update

    My Macbook can no longer connect to my wireless network at home ever since I installed a software update (NOT Snow Leopard!) at the weekend. It worked fine until I installed the automatic update, which included an Airport Update & Security Update. It

  • Jai som.vistech compilation problem

    Hello! The problem is that when I try to import a class from com.vistech, I get a "packave com.vistech.jai.imageio does not exist". The strange thing is that I can import classes form com.sun and javax.media so I suppose the installation has been mad

  • Can someone explain why this shows up when i open my macbook pro?

    It says: update necessary. And when i log in, it takes a lot of time to open, with a grey bar loading. Please help me!

  • Where is the version for 64-bit Linux?

    When I go to the Download page for Linux, it just has the 32-bit version. Of course I'm running 64-bit Linux so I don't want to run 32-bit Firefox.

  • Can't connect to printer via usb print server

    So I've tried to connect to an EPSON stylus printer from my iMac via a print server with no success. Here's some info to help focus the questions/help: iMac running Mavericks, Epson SP R200 printer, Belkin Play router, Toshiba Satellite laptop, Level