Hi,
I know there is a built in script to view server logs, but i would like to create a custom REST endpoint so non admin users can access them via the url.
Is this possible?
Thanks
Rob
Hi @Rob B ,
I guess this would be possible since you can create custom REST endpoints in groovy with scriptrunner. From there you would need to read from the log file (quick search on stackoverflow).
Antoine
Hi Antonie, Im lookin to see if there is code someone else has used so i can just change to pick the logs i need. Im not experienced with groovy so im a bit stuck.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I do not think this question has been asked before. I'll check what I can do.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
So I have figured out to return the whole log file, you need to add a custom rest end point with this code (change name to your liking) :
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response
@BaseScript CustomEndpointDelegate delegate
getLogs(httpMethod: "GET", groups: ["jira-users"]) { MultivaluedMap queryParams, String body ->
String fileContents = new File('/app/list/data/installedApps/jira/log/atlassian-jira.log').text
return Response.ok(new JsonBuilder([logs: fileContents]).toString()).build()
}
Of course update the file directory with yours. I have tried my best to come up with a script that returns the n last lines, but it seems to be pretty difficult with groovy, so I hope your log files are restrained to acceptable sizes.
You can then call the rest service with
<base-url>/rest/scriptrunner/latest/custom/getLogs
Let me know how that went.
Antoine
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You can just call an instance of the LogFileViewer class in your REST API after setting the authentication context to an admin that has permission to run such scripts.
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.canned.common.admin.LogFileViewer
def adminUser = ComponentAccessor.userManager.getUserByName('admin')
ComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(adminUser)
def logViewer = new LogFileViewer()
def inputs = [
(LogFileViewer.FIELD_LOG_FILE) : "atlassian-jira.log",
(LogFileViewer.FIELD_LOG_FILE_HMTL) : "",
(LogFileViewer.FIELD_N_LINES) : "100"
]
def result = logViewer.doScript(inputs)
return result.output
This can methodology can be used for any of the built-in scripts.
I do that to allow non-admin users to create new projects based on a specific template project (hard coded in my resp api endpoint).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Do you mean the LogFileViewer?
I just put this together on the fly in my ScriptRunner console by borrowing from other scripts.
But it would be easy to embed into a custom rest endpoint like you suggested and have the logfile and line number limit (perhaps with a hardcoded max) be passed as query parameters.
This is what I did with the the CopyProject built-in script. I embeded it in a REST API an I can control via Jira Group who has access to that endpoint.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.