|
| 1 | +--- |
| 2 | +id: macro |
| 3 | +title: macro Operator |
| 4 | +sidebar_label: macro |
| 5 | +--- |
| 6 | +import useBaseUrl from '@docusaurus/useBaseUrl'; |
| 7 | + |
| 8 | +With `macro` operator you can reuse the portions of search query language in multiple other queries. This operator also includes arguments and also performs its respective evaluation of the arguments. |
| 9 | + |
| 10 | +Use backquote (``) charater to refer macro operator in the query. Macros referred inside another macro is called **Nested macros**. Macro referred within the context of another macro query of is called **Inner Macro**. The macro where the inner macro is reffered is called the **Outer Macro**. |
| 11 | + |
| 12 | +:::note |
| 13 | +- Only **Administrators** and **Users** with access to **Query Reference** will be able to run queries using macros. |
| 14 | +- Only users with **Administrator** access can create or delete the macro. |
| 15 | +::: |
| 16 | + |
| 17 | +## Syntax |
| 18 | + |
| 19 | +``` |
| 20 | +`<macro name>` |
| 21 | +``` |
| 22 | + |
| 23 | +## Add a macro |
| 24 | + |
| 25 | +To create a macro follow the steps below: |
| 26 | + |
| 27 | +1. [**Classic UI**](/docs/get-started/sumo-logic-ui-classic). In the main Sumo Logic menu, select **Manage Data** > **Logs** > **Macros**. <br/> [**New UI**](/docs/get-started/sumo-logic-ui/). In the top menu, select **Manage Date**, and then under **Logs**, select **Macros**. You can also click the **Go To...** menu at the top of the screen and select **Macros**. |
| 28 | +1. Click **+ Add Macro**.<br/><img src={useBaseUrl('img/search/searchquerylanguage/search-operators/macro-logs-page.png')} alt="macro-logs-page" style={{border: '1px solid gray'}} width="800" /> |
| 29 | +1. Or, in the log search page, select the part of search query language that needs to be reused and click on **Create Macro**.<br/><img src={useBaseUrl('img/search/searchquerylanguage/search-operators/macro-search-page.png')} alt="macro-search-page" style={{border: '1px solid gray'}} width="800" /> |
| 30 | +1. **Macro Details**. Enter the name for the macro. Description is optional. |
| 31 | +1. **Macro Definition**. Enter the definition for the macro. To add arguments use the `{{Arg}}` syntax or select a part of the definition and click on **Add Argument**. |
| 32 | +1. (Optional)**Arguments**. Enter the name and select the data type for the argument selcted. |
| 33 | +1. (Optional)**Argument Validation**. Define the validation condition and enter the error message that needs to be shown when the validation expression returns false. |
| 34 | +1. **Usage**. Preview of how you use the macro in the log search. |
| 35 | +1. Click **Submit** to save the macro. |
| 36 | + |
| 37 | +### Limitations |
| 38 | + |
| 39 | +- You can create a maximum of 50 macros. |
| 40 | +- You can add a maximum of 5 definitions. |
| 41 | + |
| 42 | +### Example |
| 43 | + |
| 44 | +Consider the below query that search for the errors with timeslice of 5 minutes. |
| 45 | + |
| 46 | +``` |
| 47 | +_sourceCategory=error | timeslice 5m |
| 48 | +| count by _timeslice |
| 49 | +``` |
| 50 | + |
| 51 | +Now by creating macro for the timeslice field, the query with the `macro` operator is modified as: |
| 52 | + |
| 53 | +``` |
| 54 | +_sourceCategory=error | `timeslice_macro` |
| 55 | +``` |
0 commit comments