[1.0.1] How to save sikuli logs into an external file

Asked by DeepaKiran

Hi,
I am using Sikuli+TestNG+Maven with java and I run the tests as a testng suite from command line using maven command 'mvn test'.
When run tests, i see logs generated by sikuli like -
Click (280, 350), exists(280, 350) etc....
Now, I wanted to save all these logs into an external file automatically after test run, in logs.log file or .txt file.
Can someone help me in implementing this.

Thanks & Regards
Deepa

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:

This question was reopened

Revision history for this message
RaiMan (raimund-hocke) said :
#1

--1. log verbosity
there are switches, that control the log verbosity of Sikuli:
Settings.ActionLogs=true/false // starting with [log]
Settings.InfoLogs=true/false // starting with [info]

setting to false: the respective logs will be suppressed

additionally there are [error] messages in some situations, which cannot be suppressed currently.

--2. user logs (version 1.0.1+)
you can have user logs (starting with label [user] in the standard)
Settings.UserLogs=true/false // switch on/off

Settings.UserLogPrefix = "mylog" // optional: specifies the label text

usage: see the method org.sikuli.Basics.user(formattable_message, args)

--3. with timestamp (version 1.0.1+)
Sikuli logs and user logs can have time stamps at the end of the label
Settings.LogTime = true/false; // switch on/off for Sikuli logs
Settings.UserLogTime = true/false; // switch on/off for user logs (default: true)

--4. write the logs to a file (version 1.0.1+)
the class org.sikuli.basics.Debug has static methods to redirect the logs to a file:
setLogFile(String fileName); // opens a PrintStream for the given file, where all Sikuli logs are written
setUserLogFile(String fileName); // opens a PrintStream for the given file, where all user logs are written

both can be called with filename as null. Then the filename is expected in java system properties
sikuli.Logfile and sikuli.LogfileUser respectively.

Revision history for this message
DeepaKiran (deepa-kiran1116) said :
#2

Thanks RaiMan for the reply.
For now, i have manually placed a blank txt file log.txt under project directory/logs folder &
I have downloaded and added sikuli-setup.jar to my project classpath (in order to use org.sikuli.basics) and added above mentioned steps 1 and 4 into my test script. My test script now, look like below:

package dataEntry;
import java.io.IOException;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Screen;
import org.sikuli.script.Settings;
import static org.sikuli.basics.Debug.*;
import static org.testng.Assert.*;

public class OE_SP1_AFDataEntry {
 @Test
 public void TestDataEntry() throws FindFailed, IOException,
   InterruptedException {
  try
  {
  String path_or = System.getProperty("user.dir") + "\\MLAB_OR\\OE\\";
  Screen screen = new Screen();
  Settings.DebugLogs = true;
  Settings.MoveMouseDelay = 3;

  screen.wait(path_or + "de_de_btn.png",10);
  assertTrue(screen.exists(path_or + "de_de_btn.png",10) != null,"Step3: Activity list doesnot include DataEntry category");
  screen.click(path_or + "de_de_btn.png",10);
  assertTrue(screen.exists(path_or + "de_actmnu_img.PNG") != null,"Step4: Modules menu not present at right side");
  screen.click(path_or + "de_actmnu_oes_lnk.PNG");
  assertTrue(screen.exists(path_or + "de_oes_oesch_tab.PNG") != null,"Step5: Order Entry search tab is not present");
  }
  catch(Exception e)
  {
   assertTrue(false, e.toString());
   }
  setLogFile(System.getProperty("user.dir") + "\\logs\\log.txt");
 }
}

This script is executed successfully but the log.txt file is still blank. No logs have been copied to log.txt file.

Please let me know if I am doing any mistake here.

Thanks & Regards
Deepa

Revision history for this message
RaiMan (raimund-hocke) said :
#3

--1: sikuli-setup.jar in classpath ...
... not needed (and not recommended), since the package org.sikuli.basics is already contained in each of the ready-to-use jars sikuli-ide.jar, sikuli-script.jar and sikuli-java.jar

--2: catching the click log entries

package dataEntry;
import java.io.IOException;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Screen;
import org.sikuli.script.Settings;
import static org.sikuli.basics.Debug;
import static org.testng.Assert.*;

public class OE_SP1_AFDataEntry {

 static {
    Debug.setLogFile(System.getProperty("user.dir") + "\\logs\\log.txt");
 }

 @Test
 public void TestDataEntry() throws FindFailed, IOException,
   InterruptedException {
  try
  {
  String path_or = System.getProperty("user.dir") + "\\MLAB_OR\\OE\\";
  Screen screen = new Screen();
  Settings.MoveMouseDelay = 3;

  screen.wait(path_or + "de_de_btn.png",10);
  assertTrue(screen.exists(path_or + "de_de_btn.png",10) != null,"Step3: Activity list doesnot include DataEntry category");
  screen.click(path_or + "de_de_btn.png",10);
  assertTrue(screen.exists(path_or + "de_actmnu_img.PNG") != null,"Step4: Modules menu not present at right side");
  screen.click(path_or + "de_actmnu_oes_lnk.PNG");
  assertTrue(screen.exists(path_or + "de_oes_oesch_tab.PNG") != null,"Step5: Order Entry search tab is not present");
  }
  catch(Exception e)
  {
   assertTrue(false, e.toString());
   }
 }
}

Revision history for this message
RaiMan (raimund-hocke) said :
#4

I just realised, that you are using RC3 (and not 1.0.1), since

import org.sikuli.script.Settings;

only works with RC3

The Settings class has been moved to org.sikuli.basics in version 1.0+

With RC3 there is no option, to write the Sikuli logs to a file. To get this option (see comment #1) you have to switch completely to version 1.0.1 (RC3 and 1.0.1 cannot be mixed).

Revision history for this message
DeepaKiran (deepa-kiran1116) said :
#5

Thanks RaiMan,
I am trying to updated my sikuli version to 1.0.1, but facing some configuration issues. Below are the steps i followed:

1. Deleted existing old version of sikuliX folder from c:/program files
2. Created a new folder - c:/SikuliX
3. Downloaded sikuli-setup.jar in this folder
4. double clicked this jar and followed instructions in wizard
5. Now i got - libs/ , runScript.cmd, runSetup.cmd, sikuli-java.jar, sikuli-script.jar, SikuiX-1.0.1-SetupLog.txt into C:/SikuliX and set up was successful
6. I copied sikuli-java.jar and sikuli-script.jar to my projects lib folder - c:/projects/MyProject/lib
7. Added these jars to my project's classpath (c:/projects/MyProject/lib)
8. modified sikuli path in my environment system variables - SIKULI-HOME and PATH from c:/programfiles/sikuliX to c:/SikuliX
9. Now i added above :
 static {
    Debug.setLogFile(System.getProperty("user.dir") + "\\logs\\log.txt");
 }
to my script
10. When i run the script from eclipe, logs were generated in log.txt but its showing configuration error:

[error] ResourceLoaderBasic: checkLibsDir: libs dir is not on system path: C:\projects\MyProject\lib\libs
[action] ResourceLoaderBasic: checkLibsDir: Please wait! Trying to add it to user's path
[info] runcmd: reg QUERY HKCU
[info] runcmd: reg QUERY HKEY_CURRENT_USER\Environment /v PATH
[info] runcmd: reg ADD HKEY_CURRENT_USER\Environment /v PATH /t REG_EXPAND_SZ /f /d "C:\MLAB\projects\McKessonLab\lib\libs;C:\Sikuli X\libs;%MAVEN_HOME%\bin;"
[info] runcmd: reg QUERY HKEY_CURRENT_USER\Environment /v PATH
[error] ResourceLoaderBasic: checkLibsDir: Successfully added the libs folder to users PATH!
RESTART all processes/IDE's using Sikuli for new PATH to be used!/nFor usages from command line logout/login might be necessary!
[error] Terminating SikuliX after a fatal error! Sorry, but it makes no sense to continue!
If you do not have any idea about the error cause or solution, run again
with a Debug level of 3. You might paste the output to the Q&A board.

11. I also observe, the path - "C:\projects\MyProject\lib\libs" is getting added automatically to PATH variable in my environment variables. when i every time run script from eclipse

Please advice to resolve this issue

Thanks & Regards
Deepa

Revision history for this message
RaiMan (raimund-hocke) said :
#6

Steps 1 to 5 are ok.

Sikuli version 1.0.1 has these enhancements against RC3:
- the libs folder is automatically created from the active sikuli jar (sikuli-java.jar in your case) in the folder containing the jar
- on Windows the path to this libs folder is automatically added to the system path via registry (therefor you should do what is stated: logout/login might be necessary! (this is clearer in 1.1))

So it is strongly recommended, to not move around the Sikuli stuff, but leave it in the setup folder and use references instead.
(So steps 6, 7 and 8 should not happen).
Neither a manual entry to the system path nor SIKULI_HOME is needed (which is BTW SIKULIX_HOME for 1.0.1+, but not needed as well ;-)

You have to add c:\SikuliX\sikuli-java.jar to your class path (Eclipse project settings)

Revision history for this message
DeepaKiran (deepa-kiran1116) said :
#7

Thanks RaiMan, that solved my question.

Revision history for this message
DeepaKiran (deepa-kiran1116) said :
#8

Thanks Raiman, Though my problem has been resolved, I still have few questions :

1. As I am using Maven, are these sikuli-java and sikuli-script 1.0.1 jars are part of maven central repository to use as dependency in my POM ?
As my project need to refer these jars from my local sikuli's installation folder (C:/SikuliX), is it possible to use maven repository ?
Currently i am defining dependency in POM as:
<dependency>
<groupId>org.sikuli</groupId>
<artifactId>sikuli-script</artifactId>
<version>1.0.1</version>
<systemPath>C:\SukuliX\sikuli-script.jar</systemPath>
<scope>system</scope>
</dependency>

The reason i am asking this is - If I share my maven project to some of my colleague , he need to have sikuli installed in his local machine to run my project ? OR any dependency defined in POM can handle this ?

2. Can you also please give me an example code to add my own defined logs to the log.txt file. I also wanted to add few lines of text in log.txt file after each step of test execution

Revision history for this message
RaiMan (raimund-hocke) said :
#9

--- Maven Central
... SikuliX is not yet there. I will have it there after I am ready with version 1.1.0 early in 2014.

until then you either have to install it into your local repo or use a dependency declaration like yours.

BTW: since you are using Maven, I suppose you develop in plain Java. if this is true, then you do not need sikuli-script.jar. This is simply sikuli-java.jar + Jython script run support with a bundled Jython package. So if you only use Java or even Jython scripting with your own Jython installation (or IDE support) sikuli-java.jar is the only thing you need.

--- Sikuli Java apps on other machines
... if you only want to run your app on another machine with the same system (Windows in your case), you do not need any "installation".
The only thing needed is the sikuli-java.jar, that you already used.
You even might make a fat jar (Maven assembly plugin), that completely contains sikuli-java.jar.

The most ugly thing with Sikuli on Windows is the fact, that you need a ref to Sikuli's libs folder in the system path at runtime.

If it is possible, to use a command file, to start your app, then the easiest solution is to wrap the java execution into setlocal/endlocal with a PATH=<path-to-sikuli-libs>;%PATH% before the java execution, so the needed path entry is temporary for the current run (this is how the Sikuli command files do it: sikuliIDE.cmd and sikuliScript.cmd).

At runtime Sikuli first checks the existence of the libs folder. If none exists or the existing one is invalid, the libs folder usually is created in the folder, where the jar is and filled with the libs by export from the jar.

On Windows additionally it is checked, wether a ref to this libs folder is in the system path. If it is not, then Sikuli adds it via registry. But you have to restart the app or even your session (logout/ login).

BTW: on Windows, the same sikuli-java.jar works with Java 32-Bit and Java 64-Bit: at runtime simply the library set is changed dynamically via export from jar.

Revision history for this message
RaiMan (raimund-hocke) said :
#10

your question 2 from comment #8: I simply do not understand what you want to see.

Revision history for this message
DeepaKiran (deepa-kiran1116) said :
#11

Thanks RaiMan, My question 2 from comment #8 is about the user logs:
Currently, Sikuli's default logs are getting saved in log.txt and these logs look like below :

[log] CLICK on L(1064, 538)@S(0) [0,0 1920X1080]
[log] TYPE "username"
[log] CLICK on L(1064, 571)@S(0) [0,0 1920X1080]
[log] TYPE "password"
[log] CLICK on L(1074, 627)@S(0) [0,0 1920X1080]
[log] CLICK on L(93, 198)@S(0) [0,0 1920X1080]
[log] CLICK on L(1906, 991)@S(0) [0,0 1920X1080]

In addition to these logs, i want to insert my own log messages into this log.txt file which I want the log to look like :

[MyLog] Logging in to the application
[log] CLICK on L(1064, 538)@S(0) [0,0 1920X1080]
[log] TYPE "username"
[log] CLICK on L(1064, 571)@S(0) [0,0 1920X1080]
[log] TYPE "password"
[log] CLICK on L(1074, 627)@S(0) [0,0 1920X1080]
[MyLog] Clicking on Data Menu
[log] CLICK on L(93, 198)@S(0) [0,0 1920X1080]
[MyLog] Clicking on sub menu
[log] CLICK on L(1906, 991)@S(0) [0,0 1920X1080]

For generating user logs , you have mentioned the steps in Point2 of comment#1 ["usage: see the method org.sikuli.Basics.user(formattable_message, args)]
But i could not find "user" in "org.sikuli.basics"
Could you please provide a sample code to add these custom logs in to my test script.

Thanks & Regards
Deepa

Revision history for this message
RaiMan (raimund-hocke) said :
#12

I just realised, that in the current implementation, the logs from Sikuli and the user logs go to different files if logging to file is switched on.

So the only possibility, to solve your problem currently is to just log to sys-out and redirect the output to a file. For this you have to run your app from command line.

the simplest usage:
Debug.user("some message");

or adding some variables
String theImage = "some-image.png"
s.click(theImage)
Debug.user("we clicked %s at %s", theImage, s.getLastMatch())

the command line
java -jar myapp.jar >log.txt

Revision history for this message
RaiMan (raimund-hocke) said :
#13

version 1.2 will have a completely new logging concept based on logback/slf4j

Revision history for this message
DeepaKiran (deepa-kiran1116) said :
#14

Thanks RaiMan for the nice explanation.
I will wait for the version 1.2 for new logging concepts.

Can you help with this problem?

Provide an answer of your own, or ask DeepaKiran for more information if necessary.

To post a message you must log in.