Why are my Library Logs Missing in a Groovy Script? - groovy

I've been using HttpBuilder-NG in a large compiled project and relying on its request and response logging when debugging, but using it in a standalone Groovy script results in only output from my class, not the library's logs.
Can anyone suggest how to get those logs I'm expecting to see from the HttpBuilder-NG library being used?
Here's a quick test scenario I put together:
// logging-test.groovy
import ch.qos.logback.classic.Logger
import ch.qos.logback.classic.Level
import static groovyx.net.http.HttpBuilder.configure
import static groovyx.net.http.util.SslUtils.ignoreSslIssues
import groovy.util.logging.Slf4j
import groovyx.net.http.OkHttpBuilder
#GrabConfig(systemClassLoader = true) // Encounters class loading issues without this
#GrabResolver(name = 'mcArtifacts', root = 'https://artifactory.mycompany.com/artifactory/maven-all/')
#Grab(group = 'io.github.http-builder-ng', module = 'http-builder-ng-core', version = '1.0.3')
#Grab(group = 'io.github.http-builder-ng', module = 'http-builder-ng-okhttp', version = '1.0.3')
#Grab('ch.qos.logback:logback-classic:1.2.3')
#Grab('ch.qos.logback:logback-core:1.2.3')
#Slf4j
class LoggingTest {
private static final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(LoggingTest.class)
static void main(String[] args) {
new LoggingTest().run(args)
}
def run(String[] args) {
def builder = OkHttpBuilder.configure({
ignoreSslIssues execution
request.uri = "https://dummy.restapiexample.com"
})
def currentContents = builder.get {
request.uri.path = "/api/v1/employees"
}
LOGGER.info "Testing output - HttpBuilder-NG gives back a ${currentContents.getClass()}"
LOGGER.debug "Validating debug works."
}
}
And for logback configuration:
// logback.groovy
import ch.qos.logback.classic.encoder.PatternLayoutEncoder
import ch.qos.logback.core.ConsoleAppender
appender('CONSOLE', ConsoleAppender) {
encoder(PatternLayoutEncoder) {
pattern = '%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n'
}
}
logger 'groovyx.net.http.JavaHttpBuilder', DEBUG, [ 'CONSOLE' ]
logger 'groovy.net.http.JavaHttpBuilder', DEBUG, [ 'CONSOLE' ]
logger 'groovy.net.http.JavaHttpBuilder.content', TRACE, [ 'CONSOLE' ]
logger 'groovy.net.http.JavaHttpBuilder.headers', DEBUG, [ 'CONSOLE' ]
root DEBUG, ['CONSOLE']
Console output on execution:
$ groovy logging-test.groovy
08:57:46.053 [main] INFO LoggingTest - Testing output - HttpBuilder-NG gives back a class org.apache.groovy.json.internal.LazyMap
08:57:46.056 [main] DEBUG LoggingTest - Validating debug works.

Related

JMeter Groovy: print to log in class (static) (Apparent variable 'log' was found in a static scope but doesn't refer to a local...)

I want to write to log inside static class in Groovy in JMeter. I can print to terminal inside and outside of class with println or System.out.println. How to do the trick with log? In code below all works except log.warn inside class, give error:
Script53.groovy: 13: Apparent variable 'log' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes:
You attempted to reference a variable in the binding or an instance variable from a static context.
You misspelled a classname or statically imported field. Please check the spelling.
You attempted to use a method 'log' but left out brackets in a place not allowed by the grammar.
Code:
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
class CalcMain {
static void main(def args) throws Exception {
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("groovy");
println ("testing 1");
System.out.println ("testing 2");
log.warn("warn2");
}
}
OUT.println("testing 4");
println("testing 5");
log.warn("warn 1");
CalcMain test1 = new CalcMain();
test1.main();
I tried web search but could not find an answer.
You can use Groovy #Log4j annotation:
import groovy.util.logging.Log4j
#Log4j
class CalcMain {
static void main(def args) throws Exception {
// some code
log.info "hello there"
}
}
Another option is to send log as parameter to static method:
static void main(org.slf4j.Logger log) throws Exception {
Call method:
test1.main(log);
I've read https://www.blazemeter.com/blog/top-8-jmeter-java-classes-you-should-be-using-with-groovy/ where it is advised to use log variable but also gives link to Logger class. I've tried to play with Logger class and it works:
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class CalcMain {
static void main(def args) throws Exception {
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("groovy");
final Logger logger = LoggerFactory.getLogger(CalcMain.class);
logger.warn("My warning");
}
}
CalcMain test1 = new CalcMain();
test1.main();

In Robot framework how to log to console during execution

i'm actually trying to run a keyword from imported test library written in java (RF 3.0.2 , Jython 2.7.1rc3 )
import org.apache.log4j.Logger;
public class Sample
{
private static final Logger logger = Utils.getLogger(Sample.class);
#RobotKeyword("Print Message")
#ArgumentNames({"message"})
public void printMessage(String message)
{
logger.info("I'm inside");
}
}
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class Utils
{
public static final Logger logger = getLogger(Utils.class);
public static Logger getLogger(Class<?> clazz)
Logger logger = Logger.getLogger(className.getClass());
PropertyConfigurator.configure("/src/main/resources/log4j.properties");
return logger;
}
log4j.properties :
log4j.rootLogger=DEBUG, Stdout, file
log4j.appender.Stdout=org.apache.log4j.ConsoleAppender
log4j.appender.Stdout.Target=System.out
log4j.appender.Stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.Stdout.layout.conversionPattern=%d %-5p [%t] %F:%L %m%n
log4j.appender.Stdout.ImmediateFlush=true
log4j.appender.Stdout.follow=true
With this setup i'm able to see log after test execution in robot framework test report but it would be very helpful if i can see logs during test execution as if i was calling log to console keyword.
is There a way to do this ?
You can use listener interface http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#listener-interface to get real time execution information. In docs there is a sample script.
This is used in RED Robot Editor to get execution status,debug information etc. - source can be found:
TestRunnerAgent.py

Unable to load class get.spock.GebSpec due to missing dependency org/spockframework/mock/MockController

What is wrong with my test framework, that I cannot run my test?
my build.gradle
version '1.0-SNAPSHOT'
//task wrapper(type: Wrapper) {
// gebVersion = '0.13.1'
// seleniumVersion = '2.52.0'
//
// distributionUrl = "https://services.gradle.org/distributions/gradle-$gradleVersion-all.zip"
//}
apply plugin: 'groovy'
repositories {
mavenCentral()
}
sourceCompatibility = 1.5
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
// geb
compile 'org.codehaus.geb:geb-core:0.7.2'
compile 'org.codehaus.geb:geb-spock:0.7.2'
compile "org.seleniumhq.selenium:selenium-firefox-driver:2.52.0"
compile "org.seleniumhq.selenium:selenium-chrome-driver:2.52.0"
compile "org.seleniumhq.selenium:selenium-safari-driver:2.52.0"
compile "org.seleniumhq.selenium:selenium-support:2.52.0"
// spock
compile "org.codehaus.groovy:groovy-all:2.4.1"
testCompile "org.spockframework:spock-core:1.0-groovy-2.4"
}
My GebConfig.groovy
//import geb.driver.SauceLabsDriverFactory
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.safari.SafariDriver
//baseUrl = 'http://madison.craigslist.org'
// default driver...
System.setProperty('webdriver.chrome.driver', "../resources/chromedriver")
driver = {new ChromeDriver()}
environments {
'chrome' {
def chromeDriver = new File('src/test/resources/chromedriver') // add .exe for Windows...
System.setProperty('webdriver.chrome.driver', chromeDriver.absolutePath)
driver = { new ChromeDriver() }
}
// 'ff' {
// driver = { new FirefoxDriver() }
// driver.manage().window().maximize()
// }
'safari' {
driver = { new SafariDriver() }
}
}
waiting {
timeout = 6
retryInterval = 0.5
slow { timeout = 12 }
reallySlow { timeout = 24 }
}
reportsDir = "geb-reports"
page class
package pages
import geb.Page
import geb.Browser
class LoginPage extends Page{
static url = "https://qmdev.quickblox.com"
// static at = {heading.isDisplayed()}
static at = {title == "Q-municate"}
static content = {
heading {$("div#l-welcome_block l-welcome_text_description")}
logInByEmailOrSocial {$("button", text:"login by email or social")}
logInPageTitle {$("div.l-welcome_block l-welcome_text_login")}
}
}
test class
import geb.spock.GebReportingSpec
import pages.LoginPage
import spock.lang.*
import geb.spock.GebSpec
#Stepwise
class LoginPageTest extends GebReportingSpec{
def "log in Q-municate"(){
given: "Open Log In page"
to LoginPage
when: "chose log in by email"
LoginPage.logInByEmailOrSocial.click()
then: "Ensure that we are on LogIn page"
LoginPage.logInPageTitle.text() == "Log In"
}
}
About my framework:
I use web-spock-groovy-gradle bundle for web UI automation and I'm a fresh-user with Gradle.
Please tell me, what is wrong with my build.gradle and GebConfig.groovy.
your Geb-Spock version of 0.7.2 is quite old and could be the cause of your problem. I suggest changing those 2 dependencies up to version 1.1.1:
compile 'org.codehaus.geb:geb-core:0.7.2' //change to 1.1.1
compile 'org.codehaus.geb:geb-spock:0.7.2' //change to 1.1.1
Further more I check the Spock API version 1.1 for "org/spockframework/mock/MockController" and it does not appear to exist.
(http://spockframework.org/spock/javadoc/1.1-SNAPSHOT/index.html)
As you can see in this picture there are only 2 classes that are part of the spock.mock package, and MockController is not one of them. try updating your geb version and let us know if that helps

How to have Log4J print line numbers from Groovy sources

I'm writing functional tests using Geb and Spock, and building with Maven using GMavenPlus. How can I configure Log4J to to print the line numbers from my Groovy source files? This is my current log4j.properties
log4j.rootLogger=INFO, CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=DEBUG
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[%-p] %d{dd-MM-yyyy HH:mm:ss} %c:%L - %m%n
Currently this results in messages that look like
[INFO] 2015-07-28 14:13:51,589 Log4jExample:? - Some useful message
This can be reproduced in this small example
#Grab(group='log4j', module='log4j', version='1.2.17')
import org.apache.log4j.*
#groovy.util.logging.Log4j
class Log4jExample {
static void main(def args) {
log.level = Level.INFO
log.removeAllAppenders()
log.addAppender(new ConsoleAppender(new PatternLayout("[%-p] %d %c:%L - %m%n")))
log.info "Some useful message"
}
}
One solutions is to add the #CompileStatic annotation
Your example it would look like
#Grab(group='log4j', module='log4j', version='1.2.17')
import org.apache.log4j.*
#groovy.util.logging.Log4j
#CompileStatic
class Log4jExample {
static void main(def args) {
log.level = Level.INFO
log.removeAllAppenders()
log.addAppender(new ConsoleAppender(new PatternLayout("[%-p] %d %c:%L - %m%n")))
log.info "Some useful message"
}
}
Note that by using #CompileStatic the Groovy meta object protocol will be bypassed.

Spring LDAP Template Usage

Please take a look at the test class below. I am trying to do an LDAP search with Spring LDAP Template. I am able to search and produce a list of entries corresponding to the search criteria without the Spring LDAP template by using the DirContext as shown in the method searchWithoutTemplate(). But when I use a LdapTemplate, I end up with a NPE as shown further below. I am sure I must be missing something. Can someone help please?
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.LdapName;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.DefaultDirObjectFactory;
import org.springframework.ldap.core.support.LdapContextSource;
public class LDAPSearchTest {
//bind params
static String url="ldap://<IP>:<PORT>";
static String userName="cn=Directory Manager";
static String password="password123";
static String bindDN="dc=XXX,dc=com";
//search params
static String base = "ou=StandardUser,ou=XXXCustomers,ou=People,dc=XXX,dc=com";
static String filter = "(objectClass=*)";
static String[] attributeFilter = { "cn", "uid" };
static SearchControls sc = new SearchControls();
public static void main(String[] args) throws Exception {
// sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
sc.setReturningAttributes(attributeFilter);
searchWithTemplate(); //NPE
//searchWithoutTemplate(); //works fine
}
public static void searchWithTemplate() throws Exception {
DefaultDirObjectFactory factory = new DefaultDirObjectFactory();
LdapContextSource cs = new LdapContextSource();
cs.setUrl(url);
cs.setUserDn(userName);
cs.setPassword(password);
cs.setBase(bindDN);
cs.setDirObjectFactory(factory.getClass ());
LdapTemplate template = new LdapTemplate(cs);
template.afterPropertiesSet();
System.out.println((template.search(new LdapName(base), filter, sc,
new AttributesMapper() {
public Object mapFromAttributes(Attributes attrs)
throws NamingException {
System.out.println(attrs);
return attrs.get("uid").get();
}
})));
}
public static void searchWithoutTemplate() throws NamingException{
Hashtable env = new Hashtable(11);
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, url);
//env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, userName);
env.put(Context.SECURITY_CREDENTIALS, password);
DirContext dctx = new InitialDirContext(env);
NamingEnumeration results = dctx.search(base, filter, sc);
while (results.hasMore()) {
SearchResult sr = (SearchResult) results.next();
Attributes attrs = sr.getAttributes();
System.out.println(attrs);
Attribute attr = attrs.get("uid");
}
dctx.close();
}
}
Exception is:
Exception in thread "main" java.lang.NullPointerException
at org.springframework.ldap.core.support.AbstractContextSource.getReadOnlyContext(AbstractContextSource.java:125)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:287)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:237)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:588)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:546)
at LDAPSearchTest.searchWithTemplate(LDAPSearchTest.java:47)
at LDAPSearchTest.main(LDAPSearchTest.java:33)
I am using Spring 2.5.6 and Spring LDAP 1.3.0
A quick scan showed that it's the authenticationSource field of AbstractContextSource that is the culprit. That file includes the following comment on the afterPropertiesSet() method:
/**
* Checks that all necessary data is set and that there is no compatibility
* issues, after which the instance is initialized. Note that you need to
* call this method explicitly after setting all desired properties if using
* the class outside of a Spring Context.
*/
public void afterPropertiesSet() throws Exception {
...
}
That method then goes on to create an appropriate authenticationSource if you haven't provided one.
As your test code above is most definitely not running within a Spring context, and you haven't explicitly set an authenticationSource, I think you need to edit your code as follows:
...
cs.setDirObjectFactory(factory.getClass ());
// Allow Spring to configure the Context Source:
cs.afterPropertiesSet();
LdapTemplate template = new LdapTemplate(cs);

Resources