{"id":202,"date":"2015-03-25T08:42:56","date_gmt":"2015-03-25T08:42:56","guid":{"rendered":"http:\/\/smartfoxserver.com\/blog\/?p=202"},"modified":"2015-03-25T08:44:27","modified_gmt":"2015-03-25T08:44:27","slug":"using-the-trace-command-from-anywhere-in-your-extension-code","status":"publish","type":"post","link":"https:\/\/smartfoxserver.com\/blog\/using-the-trace-command-from-anywhere-in-your-extension-code\/","title":{"rendered":"Using the trace(&#8230;) command from anywhere in your Extension code"},"content":{"rendered":"<p>The trace(&#8230;) method is a useful little tool to log any information in the log files for debugging your Extension code.<\/p>\n<p>It is available in the main Extension class and in every Request or Event handler. However, often times, developers need to log messages from other places in their code such as data classes, or any other object that doesn&#8217;t extend the Request\/Event base classes.<\/p>\n<p>How can we call trace(&#8230;) from somewhere else?<!--more--><\/p>\n<p><strong>1) Refer to the Extension main class.<\/strong><\/p>\n<p>We can grab a reference to the Extension itself in the constructor of our class.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class SpaceShip\r\n{\r\n\tprivate SFSExtension ext;\r\n\tprivate float currentSpeed = 0;\r\n\r\n\tpublic SpaceShip()\r\n\t{\r\n\t\text = (SFSExtension) SmartFoxServer.getInstance().getZoneManager().getZoneByName(&quot;GameZone&quot;).getExtension();\r\n\t}\r\n\r\n\tpublic void setSpeed(float speed)\r\n\t{\r\n\t\tthis.currentSpeed = speed;\r\n\t\text.trace(&quot;Current speed is now: &quot;, currentSpeed);\r\n\t}\r\n}\r\n<\/pre>\n<p>There is \u00a0probably one\u00a0small downsides with this approach:\u00a0there is a dependency on the zone name, which could be resolved by moving the string outside of the class as a static member of another class, which takes care of the constants in the game.<\/p>\n<p><strong>2)\u00a0Inject the logger when constructing new objects.<\/strong><\/p>\n<p>Instead of referencing the Extension we could pass the Extension&#8217;s underlying logger to all those objects that need to use it.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport org.slf4j.Logger;\r\n\r\npublic class SpaceShip\r\n{\r\n\tprivate final Logger logger\r\n\tprivate float currentSpeed = 0;\r\n\r\n\tpublic SpaceShip(Logger logger)\r\n\t{\r\n\t\tthis.logger = logger;\r\n\t}\r\n\r\n\tpublic void setSpeed(float speed)\r\n\t{\r\n\t\tthis.currentSpeed = speed;\r\n\t\tlogger.info(&quot;Current speed is now: &quot; + currentSpeed);\r\n\t}\r\n}\r\n<\/pre>\n<p>This time we&#8217;re using the <a href=\"http:\/\/www.slf4j.org\/\" target=\"_blank\">SLF4J logger<\/a> instance directly, which is where\u00a0the trace command output goes, and we no longer depend on our Extension instance directly. This is probably the best approach for object reuse.<\/p>\n<p>The logger can be injected by the main class at creation time:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class MySpaceExtension extends SFSExtension\r\n{\r\n\t@Override\r\n\tpublic void init()\r\n\t{\r\n\t\t\/\/... init code ...\r\n\r\n\t\tSpaceShip mainShip = new SpaceShip(this.getLogger());\r\n\t}\r\n}\r\n<\/pre>\n<p>Regardless of what approach you will use, also keep in mind that you can work with different logging levels such as DEBUG, INFO, WARN, ERROR, to differentiate the types of output of our\u00a0log\u00a0traces.\u00a0This will be very useful to silence all unwanted debugging logs when in production.<\/p>\n<p>In order to switch the default logging level of the\u00a0Extension you can edit the <em>SFS2X\/config\/log4j.properties<\/em> file at this line:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">log4j.category.Extensions=INFO,consoleAppender,fileAppender<\/pre>\n<p>For more information on configuring the server&#8217;s logging system please <a title=\"Logging configuration\" href=\"http:\/\/docs2x.smartfoxserver.com\/GettingStarted\/log-configuration\" target=\"_blank\">refer to the documentation<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The trace(&#8230;) method is a useful little tool to log any information in the log files for debugging your Extension code. It is available in the main Extension class and in every Request or Event handler. However, often times, developers need to log messages from other places in their code such as data classes, or [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[31,36,7],"_links":{"self":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts\/202"}],"collection":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/comments?post=202"}],"version-history":[{"count":5,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts\/202\/revisions"}],"predecessor-version":[{"id":208,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts\/202\/revisions\/208"}],"wp:attachment":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/media?parent=202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/categories?post=202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/tags?post=202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}