How to add bamboo build log lines into bamboo notification template?
Asked by Abhishek S:

I am currently getting bamboo notification emails, but it doesn't have any log lines included in it, I wanted to include last 100 lines of bamboo logs into notification email once bamboo plan is completed.

I was going through [this](https://confluence.atlassian.com/bamboo/modifying-notification-templates-289276982.html) documentation to customize bamboo notification templates but I couldn't find how to fetch log lines into my freemarker template. Please help me with this, I'm new to both freemarker \& bamboo.


Answers:
author: Abhishek S
body: Hi [@Eduardo Alvarenga](/t5/user/viewprofilepage/user-id/4365410) , can you please help me with this [How to put checkout revision number of a build in ... (atlassian.com)](https://community.atlassian.com/t5/Bamboo-questions/How-to-put-checkout-revision-number-of-a-build-in-ftl-email/qaq-p/2273559) ? Was not getting any responses hence reached you.


author: Abhishek S
body: Looks like I can't delete answer, so editing it!


author: Eduardo Alvarenga
body: Hello [@Abhishek S](/t5/user/viewprofilepage/user-id/4887871), Welcome to **Atlassian Community!**

Depending on the type of notification, some may not include any build logs as they don't have that information. Here's an example showing a logging macro:

```
[#-- ============================================================================================== @nc.showLogText --]
[#macro showLogs lastLogs baseUrl buildKey ]
[#if lastLogs?has_content]

--------------
Last Logs
--------------

[#list lastLogs as log]
${log.formattedDate} |  ${log.unstyledLog}
[/#list]
[/#if]
[/#macro]
```

That is included on the common **notificationCommonsText.ftl** template. You may need to include that file on your custom one like this:

```
[#include "notificationCommonsText.ftl" ]
```

Regards,

Eduardo Alvarenga  
Atlassian Support APAC

comments: author: Abhishek S
body: Thank you [@Eduardo Alvarenga](/t5/user/viewprofilepage/user-id/4365410) !

In my custom new file, once I include macro as,

```
<@showLogs lastLogs=? baseUrl=? buildKey=?>

</showLogs>
```

what will be the values of the above variables (marked as question mark)?

I don't know how to fetch values (what are the keywords for to get logs etc.) for these variables. Can you please help me?   

An example would be highly helpful..


author: Eduardo Alvarenga
body: Hello [@Abhishek S](/t5/user/viewprofilepage/user-id/4887871),

If you are including **notificationCommonsText.ftl,** *lastLogs* , *baseUrl* and *buildKey* will be already declared correctly. So you don't have to assign a value to them.

Check **BuildHungHtmlEmail.ftl** for good examples of how to print the logs.

You can also find more info here:

* [Freemarker and notification templates](https://confluence.atlassian.com/bamboo/freemarker-and-notification-templates-289276983.html)

Kind regards,

Eduardo Alvarenga  
Atlassian Support APAC


author: Abhishek S
body: Thank you [@Eduardo Alvarenga](/t5/user/viewprofilepage/user-id/4365410) !

I created a custom template called printLogs.ftl, under bamboo-home/templates/notification-templates.  

The file has following content:

```
> cat printLogs.ftl?



[#include "notificationCommonsText.ftl" ]

[@showLogs lastLogs baseUrl buildKey /]
```

But still I'm not getting logs in the notification email. Can you please help me with this?  

I've also restarted bamboo using **sh start-bamboo.sh**


author: Eduardo Alvarenga
body: Hello [@Abhishek S](/t5/user/viewprofilepage/user-id/4887871)

The lastLogs variable is defined by the **com.atlassian.bamboo.notification.buildhung.BuildHungNotification** class in the lastLogs object context and will only work in case of hung builds notifications.

If you want to implement such logging on different notifications, you can place a direct http link to the Build logs instead.


author: Abhishek S
body: Hello [@Eduardo Alvarenga](/t5/user/viewprofilepage/user-id/4365410) , Thank you !

Placing http link looks a good idea, but my requirement is to display logs in the notification email, so I tried using iframe in the template as follows,

```
<iframe src="{bamboo-logs-url}"?style= "">

</iframe>
```

But again no logs were displayed, By any chance is there a way to bring logs when build fails?


author: Abhishek S
body: Hello [@Eduardo Alvarenga](/t5/user/viewprofilepage/user-id/4365410)

Can you please have a look at my above reply?


author: Abhishek S
body: Hello [@Eduardo Alvarenga](/t5/user/viewprofilepage/user-id/4365410)

As it seems to have no solution to my problem using existing templates, I'm thinking to create my own notification plugin. Can you please help me how to create a plugin wrt my problem? Maybe some similar plugin as example would help a lot.

Hoping for your response, Thank you!

Abhishek S


author: Eduardo Alvarenga
body: Hi [@Abhishek S](/t5/user/viewprofilepage/user-id/4887871)

You can find more information on plugin development here:

* <https://developer.atlassian.com/server/bamboo>


author: Abhishek S
body: Hi [@Eduardo Alvarenga](/t5/user/viewprofilepage/user-id/4365410)

As soon as build completes it will trigger java files (ex: BuildHungNotification.java etc), which in turn sends notifications. Am I right? If I'm wrong can you please tell me what all happens in background as we get an email notification?

Is it possible to trigger some other java files/bash script as soon as build completes?

A reply would be highly helpful.


author: Eduardo Alvarenga
body: Hello [@Abhishek S](/t5/user/viewprofilepage/user-id/4887871)

Bamboo will call the equivalent notification class depending on the action that has invoked it. For *BuildHungNotification* , Bamboo will invoke it in case the [Hanging build event](https://confluence.atlassian.com/bamboo/configuring-the-hanging-build-event-289277188.html) is triggered.

Currently, the *lastLogs* object context is only available on the BuildHungNotification class.

There's an existing feature request to enable better log inclusion into notification templates

* <https://jira.atlassian.com/browse/BAM-14912>

There are other requests for some extra functionality too:

* <https://jira.atlassian.com/browse/BAM-17085>
* <https://jira.atlassian.com/browse/BAM-18372>

Make sure to add yourself as a watcher to those tickets so you can get notified once the features are implemented.

Alternatively, you can add a new (final) Stage to your build plan that would contain a script task within a job that would fetch the build logs and build state (success/failure) from the past Jobs in that Plan using the Bamboo REST API and send their logs to your email recipients. Make sure to also exit that final Job within that Stage with the same return code provided by the last Job so it can match success/failure.

Regards,

Eduardo Alvarenga  
Atlassian Support APAC


author: Abhishek S
body: Hi [@Eduardo Alvarenga](https://community.atlassian.com/t5/user/viewprofilepage/user-id/4365410) , thanks for the reply!  

> Bamboo will call the equivalent notification class depending on action that has invoked it.

Where are these classes located in the installation directory?
> Alternatively, you can add a new (final) Stage to your build plan that would contain a script task within a job that would fetch the build logs.

This is an option to consider, but can you please tell me how to fetch build logs inside task? (if through api please share it..)


author: Eduardo Alvarenga
body: [@Abhishek S](/t5/user/viewprofilepage/user-id/4887871)

The classes are compiled and cannot be easily read in the Bamboo installation directory. You can explore the Bamboo source code via <https://my.atlassian.com/download/source/bamboo> (need a proper licence)

For reference, the classes are the ones listed here:

* <https://docs.atlassian.com/bamboo-specs/8.2.2/com/atlassian/bamboo/specs/api/builders/notification/NotificationType.html>

To fetch build logs you can use cURL GET/POST requests, more details here:

* <https://docs.atlassian.com/atlassian-bamboo/REST/8.2.2/#d2e32>
* <https://developer.atlassian.com/server/bamboo/using-the-bamboo-rest-apis/>

Regards,

Eduardo Alvarenga  
Atlassian Support APAC


Tags: freemarker