Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to access log for confluence when writing code in scriptrunner?

Mouna Hammoudi
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 3, 2023

I am writing code in scriptrunner and logging some information but what I am logging is too long, so how can I access the full confluence log? 

Capture.PNG

Here is a picture of my log above, only the first 300 lines are shown as you can see above.

 

Here is my code below: 

import com.atlassian.confluence.links.OutgoingLink

import com.atlassian.confluence.pages.Page

import com.atlassian.confluence.pages.PageManager

import com.atlassian.confluence.spaces.Space

import com.atlassian.confluence.spaces.SpaceManager

import com.atlassian.sal.api.component.ComponentLocator

import org.apache.log4j.Logger

import com.atlassian.confluence.user.UserAccessor

import com.atlassian.sal.api.user.UserKey

SpaceManager spaceManager = ComponentLocator.getComponent(SpaceManager)

PageManager pageManager = ComponentLocator.getComponent(PageManager)

UserAccessor userAccessor = ComponentLocator.getComponent(UserAccessor)

Space space = spaceManager.getSpace("IWIKI")

for (Page page : pageManager.getPages(space, true)) {

    if(page.getCreator()==null){

            log.warn(page.toString()+",null")

    }

    else{

            String userID=page.getCreator().getName()

            String fullName =userAccessor.getUserByKey(page.getCreator().getKey()).getFullName()

            log.warn(page.toString()+","+userID+","+fullName+","+page.getLastModificationDate())

    }

   

}


 

1 answer

1 vote
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
July 3, 2023

Hi @Mouna Hammoudi

The ScriptRunner console output has only a limit of up to 300 lines; the rest would be truncated.

I suggest writing your result to a file, i.e. something like:-

import com.atlassian.confluence.links.OutgoingLink
import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.spaces.SpaceManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.user.UserAccessor
import com.atlassian.sal.api.user.UserKey

def spaceManager = ComponentLocator.getComponent(SpaceManager)
def pageManager = ComponentLocator.getComponent(PageManager)
def userAccessor = ComponentLocator.getComponent(UserAccessor)
def space = spaceManager.getSpace("IWIKI")

pageManager.getPages(space, true).each { page ->

    if(!page.creator){

        log.warn("${page.toString()},null")

    } else {
def userID = page.creator.name
          def fullName = userAccessor.getUserByKey(page.creator.key).fullName
def file = new File('/temp/result.log')
def fileWriter = new FileWriter(file)
fileWriter.write("${page.toString(), ${userID}, ${fullName}, ${page.lastModificationDate}")
fileWriter.close()
    }
}

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

I hope this helps to solve your question. :-)

Thank you and Kind regards,

Ram 

Evgenii
Community Champion
July 3, 2023

As an alternative, I can suggest concatenation to one loooooong string, with <br/> delimiters, and then output it to Result tab.

Mouna Hammoudi
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 3, 2023

@Ram Kumar Aravindakshan _Adaptavist_ 

 

Is it not possible to access a log instead of writing it to a file? 

 

I clicked on Logging and Profiling under the admin section of Confluence and I got the following: 

Capture.PNG

 

I specified the same string highlighted in yellow as my log destination.

 

Here is the code: 



import com.atlassian.confluence.links.OutgoingLink

import com.atlassian.confluence.pages.Page

import com.atlassian.confluence.pages.PageManager

import com.atlassian.confluence.spaces.Space

import com.atlassian.confluence.spaces.SpaceManager

import com.atlassian.sal.api.component.ComponentLocator

import org.apache.log4j.Logger

import com.atlassian.confluence.user.UserAccessor

import com.atlassian.sal.api.user.UserKey



SpaceManager spaceManager = ComponentLocator.getComponent(SpaceManager)

PageManager pageManager = ComponentLocator.getComponent(PageManager)

UserAccessor userAccessor = ComponentLocator.getComponent(UserAccessor)

def log = Logger.getLogger("com.atlassian.bonnie.search.extractor")

Space space = spaceManager.getSpace("IWIKI")

for (Page page : pageManager.getPages(space, true)) {

    if(page.getCreator()==null){

            log.warn(page.toString()+",null")

    }

    else{

            String userID=page.getCreator().getName()

            String fullName =userAccessor.getUserByKey(page.getCreator().getKey()).getFullName()

            log.warn("mouna"+","+page.toString()+","+userID+","+fullName+","+page.getLastModificationDate())

    }

   

} 
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
July 3, 2023

Hi @Mouna Hammoudi

In your last comment, you asked:-

Is it not possible to access a log instead of writing it to a file? 

To answer your question, yes, it is possible to write to a log if that is your preference.

If you intend to pass it all into the logs, I would suggest adding a Marker into the logs so it would be easier to trace something like:-

import com.atlassian.confluence.links.OutgoingLink
import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.spaces.SpaceManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.user.UserAccessor
import com.atlassian.sal.api.user.UserKey

def spaceManager = ComponentLocator.getComponent(SpaceManager)
def pageManager = ComponentLocator.getComponent(PageManager)
def userAccessor = ComponentLocator.getComponent(UserAccessor)
def space = spaceManager.getSpace("IWIKI")

pageManager.getPages(space, true).each { page ->

    if(!page.creator){

        log.warn("${page.toString()},null")

    } else {
def userID = page.creator.name
          def fullName = userAccessor.getUserByKey(page.creator.key).fullName
log.warn "===================== START ======================"
log.warn "mouna, ${page.toString(), ${userID}, ${fullName}, ${page.lastModificationDate}"
log.warn "===================== END ======================"

    }
}

You will then need to look into the atlassian-confluence.log file to check the output printed.

However, if you expect all the results to be displayed in the ScriptRunner Console, that will not happen, as it will truncate the result.

I hope this helps to solve your question. :-)

Thank you and Kind regards,

Ram

Like Evgenii likes this
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
July 5, 2023

Hi @Mouna Hammoudi

Has your question been answered?

If yes, please accept the answer.

Thank you and Kind regards,
Ram

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events