I wanna print a few values in the console, how do I do this?
Every time I get into a function, I want it to print a line with whatever text, I just wanna know if I'm getting into the function, and for some if-else statements.
Mostly for debugging.
If you mean "print to the console output panel", then you simply need to use println:
println "Hello, world"
Results in printed output:
groovy> println "Hello, world"
Hello, world
If that's not what you mean, can you clarify your question to be more specific?
you might want to consider grails built in logging functionality which provides the same functionality as println plus more
http://grails.github.io/grails-doc/3.0.x/guide/single.html#logging
in your app just say
log.info "Hello World"
to print something everytime you enter an action in a controller you can do something like this
class UserController {
def beforeInterceptor = {
log.info "Entering Action ${actionUri}"
}
def index = {
}
def listContributors = {
}
}
this will print out to the log whenever the controller methods are entered because of the controller interceptor
The regular java System.out.println("Your stuff"); works too.
Related
I need some help. I'm a noob in Jenkins and Groovy.
How to get a specific string from the log containing a certain word and print?
def log = Jenkins.getInstance().getItemByFullName
('deploy/my_deploy').getBuildByNumber(checkjob.getNumber()).log
if (log.contains("Word")) {println log}
Not sure about the "log" type Jenkins sends back, but for instance you can try the findAll method, like this :
def log = [ "First line", "Second line", "End of log"]
println log.findAll { line -> line.contains("line") }
>>> [First line, Second line]
Which iterates through the string list (log), and for every line in it, take it if the condition ( line.containts("line") ) is true
Currently, im using findAll. But I dont need the list that it returns. return doesnt work in each so I could not use it.
In grails, is there a loop that matched my need or should I use for loop?
If it makes any sense I believe you are trying to use it as a state rather than wanting the results in which case I doubt you would need findAll
so something like
def user = User.findByUsername('username')
Now
if you did
if (user) {
//do something
}
That would tell you there is something found or maybe:
int size = (User.findAllByUsername('username')?.size()) ?: 0
println "found ${size} records"
Ofcourse if you did
User.findAll{}
Thats all then you iterate through it find what you want
instead if you did
//def aa = User.findAll{user=='username'}?.size()
def aa = User.findAll{user=='username'}
if (aa ) {
println "we have something "
}
I think the issue is with the UrlMapping file or some configuration file that I don't know about but I didn't see it addressed in this site so I'm posting for help.
I have a UrlMappings.groovy with:
"/lookupMap/$fromVal/$toVal/xml/$id**" (controller:"lookup, action:"returnMapXml", formats=['xml'], method:"GET")
and the controller is:
def returnMapXml = {
if (params.id) {
print params.id + "\n";
try {
def result = getLookup.result(params.fromVal, params.toVal, params.id)
render ...yadda yadda
}
}
}
This is a REST service. My problem happens when someone enters an ID value with either a pound sign (#) or question mark (?), the value is truncated at that character. For example the output of ID (per the print line in the code) for this: localhost:8080/productdefinition/lookupMap/Denver/Toronto/carton OR container OR box? OR bag would be carton OR container OR box It removes the ? and everything after it. This happens somewhere either before it gets to the UrlMappings file or when that directs the call to the controller. Either way, how can I stop this and where, which file do I fix this in? I don't have access to the server so I can't alter any URL encodings; this has to be a code update. Any help/direction would be appreciated.
I have a Rails app parsing incoming e-mails on Heroku using the Cloud-mailin add-on. The app recieves a list of prices in an e-mail and inserts them into the database.
This works fine, but if the e-mail contains for instance a signature in the bottom the code fails because it's also trying to parse that text.
Therefor I would like to rewrite the below parsing code to stop when it hits an empty line in the e-mail. All the price data is always at the top of the e-mail.
email_text = params[:plain]
email_text_array = []
email_text.split("\n").each do |email_line|
email_text_array << email_line.split(" ")
end
How do I change the above to stop when it hits an empty line in the email_taxt variable?
Thanks!
You can add a break :
email_text.split("\n").each do |email_line|
break if email_line.blank? # ends loop on first empty line
email_text_array << email_line.split(" ")
end
Does this question help: Is there a "do ... while" loop in Ruby?
Edit 1:
From the above article I think something like this would work:
email_text.split("\n").each do |email_line|
break if email_line.length < 1
email_text_array << email_line.split(" ")
end
I'm working with an example that I can't understand what the braces do -- the ones around the "Logout" in the second "out" statement below. I guess the string is passed as a closure but I'm not getting the syntax beyond that. Can you please clarify? Note the output of the code looks like the following:
John Doe [Logout]
class LoginTagLib {
def loginControl = {
if(request.getSession(false) && session.user){
out << "Hello ${session.user.login} "
out << """[${link(action:"logout",
controller:"user"){"Logout"}}]"""
} else {
out << """[${link(action:"login",
controller:"user"){"Login"}}]"""
}
}
}
Thanks Much
The link tag takes attributes and a body, and as a regular GSP tag it's called like this:
<g:link action="logout" controller="user">Logout</g:link>
To invoke it as a method like you're doing, you need a way to pass the text ('Logout') to render in the link. If you look at the source of the tag (click "Show Source" at the bottom of http://grails.org/doc/latest/ref/Tags/link.html) you'll see that the 2nd argument is body, and it's a Closure (although that's not clear from the code, but that's always the case for 2-parameter tags). {"Logout"} is a Closure that returns "Logout" since it's the last expression, so it's used as the body.
Actually the output should be
Hello John Doe [Logout]
Essentially, if there is a session and a user write Hello user and create a link pointing to a logout action with the label Logout.
{ "Logout" } is a closure equivalent to { return "Logout"; } as the last statement is used for a return value if none is explicitly stated.
I am not able to get the output like below
Hello John Doe [Logout]
Here is the output I am getting
Hello jdoe [Logout