PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/tutorial/chapters/Tutorial4.md

https://gitlab.com/vikash.patel/qwertyu
Markdown | 133 lines | 92 code | 41 blank | 0 comment | 0 complexity | f416adccecb6ece965e0b70c048c9424 MD5 | raw file
  1. Previous: [Create a Composite Widget](./Tutorial3.md),
  2. Next: [Variable Substitutions in Models](./Tutorial5.md)
  3. ## Tutorial 4 - Configuring Logging and Debug
  4. Before we get too far into these tutorials its worth spending some time looking at what to do when things go wrong during development. There are already some great tools in Aikau for debugging and we will continue to add more as the framework develops. In this tutorial were going to look at the `alfresco/services/LoggingService` and `alfresco/logging/SubscriptionLog` modules.
  5. ### Logging Service
  6. The `alfresco/services/LoggingService` acts as a layer of abstraction between widgets and the browser console. The `alfresco/core/Core` module that is mixed into every Aikau widget and service provides an `alfLog` function which when called will publish logging information that the `alfresco/services/LoggingService` will handle.
  7. If youre wondering why widgets dont call the `console` object of the browser directly its because this layer of abstraction allows us to filter logging (as well see later) as well as provide other services to address logging. For example the Alfresco Share client can use the `alfresco/services/ErrorReporter` service to capture client side errors and POST them back to the web server. In this way administrators can track errors that users might report.
  8. We can include the `alfresco/services/LoggingService` on our pages by adding it to the services array in the page model like this:
  9. ```JAVASCRIPT
  10. model.jsonModel = {
  11. services: [
  12. {
  13. name: "alfresco/services/LoggingService",
  14. config: {
  15. loggingPreferences: {
  16. enabled: true,
  17. all: true
  18. }
  19. }
  20. },
  21. ```
  22. **NOTE: Here we are adding the LoggingService into the services array as an object rather than as a string. The majority of services don't require configuration and can just added as strings, however widgets must always be added as objects.**
  23. In this particular example we are defining the level of logging required but it is also possible to configure the widget to access the current users logging preferences from the Alfresco Repository - you just need to remove the `config` section completely and include the `alfresco/services/PreferenceService` on the page as well.
  24. When you load a page containing this configuration and inspect the browser console (usually done by pressing the F12 key) youll see logging appear. Try it with the home page to see:
  25. ![Image showing screenshot of browser console](../resources/Tutorial4-Image1.png "Image showing screenshot of browser console")
  26. ### Adding Logging Code
  27. Lets add some logging code to the `tutorial/HelloWorld` widget, add the following into the `postMixInProperties` function.
  28. ```JAVASCRIPT
  29. this.alfLog("log", "Setting greeting message!");
  30. ```
  31. Because you are making a change to a widget file you will need to clear the old version of the file that will have previously been cached. To do this go to the following URL: [http://localhost:8090/aikau-sample/service/index](http://localhost:8090/aikau-sample/service/index "Link to service index page").
  32. ...and click on the button marked Clear Dependency Caches.
  33. **IMPORTANT: Each time you make a change to a JavaScript, CSS, HTML or properties file related to a widget or service you will need to clear the caches in order to see the change without restarting the server. More information on Aikau caching can be found [here](./ClearingDependencyCaches.md)**
  34. Now refresh the page and amongst all the logging will be the output from the HelloWorld widget.. hard to find wasnt it?
  35. ![Browser console with highlighted log from HelloWorld widget](../resources/Tutorial4-Image2.png "Browser console with highlighted log from HelloWorld widget")
  36. Fortunately we can configure a filter on the LoggingService to only output messages from specific packages, modules or even functions. If you wondered why the HelloWorld widget was given a specific function name then this is why.
  37. Update the `config` object of the LoggingService so that it looks like this:
  38. ```JAVASCRIPT
  39. {
  40. name: "alfresco/services/LoggingService",
  41. config: {
  42. loggingPreferences: {
  43. enabled: true,
  44. all: true,
  45. filter: "tutorial/HelloWorld(.*)"
  46. }
  47. }
  48. }
  49. ```
  50. Refresh the page and look for the message in the browser console... bit easier to find this time wasnt it?
  51. ![Browser console with filtered log showing only HelloWorld widget logging](../resources/Tutorial4-Image3.png "Browser console with filtered log showing only HelloWorld widget logging")
  52. The filter attribute allows us to set a Regular Expression to match the logged message. Hopefully youve noticed that the console very clearly indicates which module and function has issued the log request.
  53. When youre debugging a specific widget or function this facility can be particularly useful to filter out messages from widgets that you arent interested in.
  54. ### Log Levels
  55. When using the alfLog function the first argument is the log level that you wish to use. This maps to the functions provided by most browser `console` objects. Typically youll want to use "log" ...but you might also want to use info, warn and error depending upon the severity of what your widget is reporting, e.g:
  56. ```JAVASCRIPT
  57. this.alfLog("error", "Something has gone terribly wrong!");
  58. ```
  59. The LoggingService can be quickly configured to only display warning and error messages by setting the all attribute to false and then setting boolean values for each specific log level, e.g. the configuration:
  60. ```JAVASCRIPT
  61. {
  62. name: "alfresco/services/LoggingService",
  63. config: {
  64. loggingPreferences: {
  65. enabled: true,
  66. all: false,
  67. warn: true,
  68. error: true
  69. }
  70. }
  71. }
  72. ```
  73. ...will only output warn and error messages.
  74. ![Browser console with filtered log showing only warning logs](../resources/Tutorial4-Image4.png "Browser console with filtered log showing only warning logs")
  75. ### Dynamically Updating Logging Settings
  76. The LoggingService subscribes to the `ALF_UPDATE_LOGGING_PREFERENCES` topic to allow you to add widgets that let the user dynamically control the log levels and filter that is currently in action. Well look into making use of the publication/subscription communication model in much more detail in later tutorials - but its worth remembering that this is possible. An example of this can be seen in the Alfresco Share client when running in debug mode.
  77. ![Screenshot of Alfresco Share with debug menu open](../resources/Tutorial4-Image5.png "Screenshot of Alfresco Share with debug menu open")
  78. ### Publication/Subscription Logging
  79. The primary purpose for the `alfresco/logging/SubscriptionLog` widget is for use in unit testing other widgets. When running in client-debug mode (as your client has been initially configured to do) it will log all publications and subscription that are performed using the `alfPublish` and `alfSubscribe` functions (provided by the `alfresco/core/Core` module).
  80. However, the SubcriptionLog can also be used as debug and can be displayed in the Alfresco Share client by clicking on the the Show Pub/Sub Log menu item in the Debug Menu. When developing a page it can simply be added into the main `widgets` array of your WebScript controller.
  81. Add the following as the last entry into root `widgets` attribute in the `<PROJECT>/src/main/webapp/WEB-INF/webscripts/pages/home.get.js` file:
  82. ```JAVASCRIPT
  83. {
  84. name: "alfresco/logging/SubscriptionLog"
  85. }
  86. ```
  87. And you should see the following (when you scroll down to the bottom of the page)
  88. ![Screenshot showing SubcriptionLog output](../resources/Tutorial4-Image6.png "Screenshot showing SubcriptionLog output")
  89. Using this log is one of the best ways of seeing exactly how widgets are attempting to communicate with one another and is a useful tool to use when trying to get to resolve issues on your page.
  90. _You can [download the project files from here](../resources/aikau-tutorial_004.zip?raw=true) as they exist at the end of this tutorial._
  91. Previous: [Create a Composite Widget](./Tutorial3.md),
  92. Next: [Variable Substitutions in Models](./Tutorial5.md)