Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 2 FirstFirst 12
Results 26 to 36 of 36

Thread: can't seem to find the log

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: can't seem to find the log

    Also these questions about how the Logger program is used in the Dukascopy environment:
    How does your code call the Logger class's constructor? What statements have: new Logger() in them?
    How does your code call the Logger class's methods?
    Why do you think the Logger class is usable? Where did you get it?

    If you did not write the code, how did it get this statement in it?
    private String   logFile  = "C:/Users/Bob/Documents/BM_mod14.log";
    What was in the code before you added that statement? If you look at the code in the constructor you can see that the value of logFile is replaced:
              logFile = lf;
    so giving it any value is useless because it is replaced in the constructor.

    --- Update ---

    Try this:
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.OutputStreamWriter;
    import java.io.*;    //IOException;
    import java.util.HashSet;
    import java.util.Vector;
    import java.text.DecimalFormat;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.List;
    import java.util.Set;
    import java.util.TimeZone;
     
     class IConsole {  
               PrintStream getOut() {
                   return System.out;
               }
      }      
     
    class Logger {
     
            private IConsole console = null;
            private String   logFile  = "C:/Users/Bob/Documents/BM_mod14.log";
            private boolean  info     = true;
            private boolean  error    = true;
            private boolean  debug    = true;
     
     
            public Logger(IConsole cons, String lf, boolean inf, boolean err, boolean dbg) {
                console = cons;
                logFile = lf;
                info    = inf;
                error   = err;
                debug   = dbg;    
                if (!"".equals(lf)) new File(logFile).delete();
            }
     
            public void info(String Data) {
                if (info) {
                    Data = "INFO  : " + Data;
                    Log(Data);
                }
            }
     
            public void error(String Data) {
                if (error) {
                    Data = "ERROR :  " + Data;
                    Log(Data);                
                }            
            }
     
            public void debug(String Data) {
                if (debug) {
                    Data = "DEBUG :  " + Data;
                    Log(Data);                
                }            
            }
     
            private void Log(String Data) {
                 try {
                     if (console != null) console.getOut().println(Data);
                     LogToFile(Data);
                 } catch (Exception e) {
                 }
             }    
     
             private void LogToFile(String Data) {
                 try {    
                     if ("".equals(Data)) return;
                     if ("".equals(logFile)) return;
     
                     File                 file = new File(logFile);                                
                     FileOutputStream     fos  = new FileOutputStream(file, true);
                     OutputStreamWriter   osw  = new OutputStreamWriter(fos, "Cp1252" /* "ISO-8859-1" */);
                     BufferedWriter       bw   = new BufferedWriter(osw);
                     try {
                         bw.write(Data + "\n");
                     } finally {
                         try {
                             bw.flush();
                             bw.close();
                         } catch (IOException e) {
                             System.out.println("Error: " + e.getMessage());
                             e.printStackTrace();
                         }
                         try {
                             osw.flush();
                             osw.close();
                         } catch (IOException e) {
                             System.out.println("Error: " + e.getMessage());
                             e.printStackTrace();
                         }
                         try {
                             fos.flush();
                             fos.close();
                         } catch (IOException e) {
                             System.out.println("Error: " + e.getMessage());
                             e.printStackTrace();
                         }
                     }
                 } catch (IOException e) {
                        System.out.println("Error: " + e.getMessage());
                        e.printStackTrace();
                         }
     
             } // end LogToFile
     
          public static void main(String[] args) {
             //  add code here to declare an instance of the logger class
             Logger l = new Logger(new IConsole(), "BobsLog.txt", false, false, false);
             l.Log("This is  a test 2");
     
     
         }  // end main
     
     } // end of class Logger
    If you don't understand my answer, don't ignore it, ask a question.

  2. #27
    Member
    Join Date
    Mar 2021
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: can't seem to find the log

    error: can't find main(String[]) method in class: IConsole

    --- Update ---

    [QUOTE=Norm;171051]Also these questions about how the Logger program is used in the Dukascopy environment:

    Why do you think the Logger class is usable? Where did you get it?

    it forms part of the overall strategy

    If you did not write the code, how did it get this statement in it?
    private String   logFile  = "C:/Users/Bob/Documents/BM_mod14.log";

    I added the details

    What was in the code before you added that statement?
    private String logFile = "";

  3. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: can't seem to find the log

    You keep skipping these questions:
    How does your code call the Logger class's constructor? What statements have: new Logger() in them?
    How does your code call the Logger class's methods?
    error: can't find main(String[]) method in class: IConsole
    The command needs to be: java Logger
    not Java IConsole.

    Change the source by putting the IConsole class at the end of the source file.
    Also add the word public before the class Logger: public class Logger {
    If you don't understand my answer, don't ignore it, ask a question.

  4. #29
    Member
    Join Date
    Mar 2021
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: can't seem to find the log

    How does your code call the Logger class's constructor? What statements have: new Logger() in them?

    Logger l = new Logger(new IConsole(), "BobsLog.txt", false, false, false

    How does your code call the Logger class's methods?
    logger.info(FormatDate(askBar.getTime()) +  " LEVERAGE CONTROL: " + uol + " -- Orders closed");
     
    logger.info(FormatDate(tick.getTime()) + ": openOrder " + label + " " + FormatString(command.toString(), 4) + " --> lot: " + FormatFloat(Amount) + " price: " + FormatFloat(price) + ", TakeProfit: " + FormatFloat(TakeProfit) + ", StopLoss: " + FormatFloat(StopLoss));
     
    logger.error(e.getMessage());
     
    logger.debug(FormatDate(tick.getTime()) + " ENTER_LONG ("+instrument.toString()+")  - NOT ALLOWED");

    Error:-
    c:\Users\rgmat\Desktop>java logger.java
    logger.java:106: error: non-static variable this cannot be referenced from a static context
    Logger l = new Logger(new IConsole(), "BobsLog.txt", false, false, false);
    ^
    1 error

    Current code:-
            import java.io.BufferedWriter;
            import java.io.File;
            import java.io.FileOutputStream;
            import java.io.OutputStreamWriter;
            import java.io.*;    //IOException;
            import java.util.HashSet;
            import java.util.Vector;
            import java.text.DecimalFormat;
            import java.text.SimpleDateFormat;
            import java.util.Calendar;
            import java.util.Date;
            import java.util.List;
            import java.util.Set;
            import java.util.TimeZone;
     
            public class Logger {
     
                    private IConsole console = null;
                    private String   logFile  = "";
                    private boolean  info     = true;
                    private boolean  error    = true;
                    private boolean  debug    = true;
     
     
                    public Logger(IConsole cons, String lf, boolean inf, boolean err, boolean dbg) {
                        console = cons;
                        logFile = lf;
                        info    = inf;
                        error   = err;
                        debug   = dbg;    
                        if (!"".equals(lf)) new File(logFile).delete();
                    }
     
                    public void info(String Data) {
                        if (info) {
                            Data = "INFO  : " + Data;
                            Log(Data);
                        }
                    }
     
                    public void error(String Data) {
                        if (error) {
                            Data = "ERROR :  " + Data;
                            Log(Data);                
                        }            
                    }
     
                    public void debug(String Data) {
                        if (debug) {
                            Data = "DEBUG :  " + Data;
                            Log(Data);                
                        }            
                    }
     
                    private void Log(String Data) {
                         try {
                             if (console != null) console.getOut().println(Data);
                             LogToFile(Data);
                         } catch (Exception e) {
                         }
                     }    
     
                     private void LogToFile(String Data) {
                         try {    
                             if ("".equals(Data)) return;
                             if ("".equals(logFile)) return;
     
                             File                 file = new File(logFile);                                
                             FileOutputStream     fos  = new FileOutputStream(file, true);
                             OutputStreamWriter   osw  = new OutputStreamWriter(fos, "Cp1252" /* "ISO-8859-1" */);
                             BufferedWriter       bw   = new BufferedWriter(osw);
                             try {
                                 bw.write(Data + "\n");
                             } finally {
                                 try {
                                     bw.flush();
                                     bw.close();
                                 } catch (IOException e) {
                                     System.out.println("Error: " + e.getMessage());
                                     e.printStackTrace();
                                 }
                                 try {
                                     osw.flush();
                                     osw.close();
                                 } catch (IOException e) {
                                     System.out.println("Error: " + e.getMessage());
                                     e.printStackTrace();
                                 }
                                 try {
                                     fos.flush();
                                     fos.close();
                                 } catch (IOException e) {
                                     System.out.println("Error: " + e.getMessage());
                                     e.printStackTrace();
                                 }
                             }
                         } catch (IOException e) {
                                System.out.println("Error: " + e.getMessage());
                                e.printStackTrace();
                                 }
     
                     } // end LogToFile
     
                  public static void main(String[] args) {
                     //  add code here to declare an instance of the logger class
                     Logger l = new Logger(new IConsole(), "BobsLog.txt", false, false, false);
                     l.Log("This is  a test 2");
     
     
                 }  // end main
     
    		 class IConsole {  
                       PrintStream getOut() {
                           return System.out;
                       }
              }      
     
     
             } // end of class Logger
    Last edited by BobM; March 18th, 2021 at 08:23 PM.

  5. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: can't seem to find the log

    Move the IConsole class's definition outside of the Logger class (past the line: } // end of class Logger) at the end of the source file.

    How does your code call the Logger class's constructor? What statements have: new Logger() in them?

    Logger l = new Logger(new IConsole(), "BobsLog.txt", false, false, false
    That is my code for testing.
    What is in your code on the Dukascopy system? How do you expect to use the Logger class in the Dukascopy system?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #31
    Member
    Join Date
    Mar 2021
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: can't seem to find the log

    in the onStart method in the strategy
    logger              = new BM_mod14.Logger(console, "", true, true, false);

    output current:-
    c:\Users\rgmat\Desktop>java logger.java
    This is a test 2
    Error: Stream closed
    java.io.IOException: Stream closed
    at java.base/sun.nio.cs.StreamEncoder.ensureOpen(StreamEncoder. java:51)
    at java.base/sun.nio.cs.StreamEncoder.flush(StreamEncoder.java: 158)
    at java.base/java.io.OutputStreamWriter.flush(OutputStreamWrite r.java:251)
    at Logger.LogToFile(logger.java:83)
    at Logger.Log(logger.java:58)
    at Logger.main(logger.java:107)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invo ke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invo ke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl. invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at jdk.compiler/com.sun.tools.javac.launcher.Main.execute(Main.jav a:415)
    at jdk.compiler/com.sun.tools.javac.launcher.Main.run(Main.java:19 2)
    at jdk.compiler/com.sun.tools.javac.launcher.Main.main(Main.java:1 32)

  7. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: can't seem to find the log

    new BM_mod14.Logger(console, "", true, true, false);
    That statement says the path to the logger file is "". Where is that location????

    Note this statement:
                      if ("".equals(logFile)) return;

    Try this with a good path in the original code:
       new BM_mod14.Logger(console, "GoodPath.log", true, true, false);

    java.io.IOException: Stream closed
    at java.base/sun.nio.cs.StreamEncoder.ensureOpen(StreamEncoder. java:51)
    at java.base/sun.nio.cs.StreamEncoder.flush(StreamEncoder.java: 158)
    at java.base/java.io.OutputStreamWriter.flush(OutputStreamWrite r.java:251)
    at Logger.LogToFile(logger.java:83)
    That says the code calls the flush method on line 83 for an output stream that is already closed.
    The code has 3 calls to the close method but it only needs one. Remove the other 2 calls to close.

    Was the log file created and written to?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #33
    Member
    Join Date
    Mar 2021
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: can't seem to find the log

    yes - logfile is written to

    bw.close(), fos.close() and osw.close()

    you suggest I delete 2 of the above
    do I leave all 3 flushes ?

  9. #34
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: can't seem to find the log

    I think only 1 close is required. The API doc for the close method says: Closes the stream, flushing it first.

    yes - logfile is written ...
    Have you solved the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  10. The Following User Says Thank You to Norm For This Useful Post:

    BobM (March 19th, 2021)

  11. #35
    Member
    Join Date
    Mar 2021
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: can't seem to find the log

    yes - you have solved my problem

    I am sorry it has been a long drawn out process

    Many, many thanks

    Bob M
    Dunedin, New Zealand

  12. #36
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: can't seem to find the log

    Great. I am glad you got it working.
    If you don't understand my answer, don't ignore it, ask a question.

  13. The Following User Says Thank You to Norm For This Useful Post:

    BobM (March 19th, 2021)

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Can't find the out put
    By javab4 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 4th, 2013, 05:07 PM
  2. Is there a way to find the url of the applet?
    By micro kid in forum Java Applets
    Replies: 1
    Last Post: June 11th, 2012, 07:21 AM
  3. Cannot find symbol
    By BadgerWatch in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 6th, 2011, 11:25 PM
  4. Cannot find symbol?
    By stealthmonkey in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 10th, 2010, 10:02 PM
  5. find LCS
    By Anemone_ in forum Algorithms & Recursion
    Replies: 1
    Last Post: November 28th, 2010, 02:03 PM