<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Advanced Logging &#8211; Xojo Programming Blog</title>
	<atom:link href="https://blog.xojo.com/tag/advanced-logging/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.xojo.com</link>
	<description>Blog about the Xojo programming language and IDE</description>
	<lastBuildDate>Thu, 28 Nov 2024 10:26:45 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Log4Xojo: A more powerful way to manage your app logging</title>
		<link>https://blog.xojo.com/2024/11/26/log4xojo-a-more-powerful-way-to-manage-your-app-logging/</link>
		
		<dc:creator><![CDATA[Gabriel Ludosanu]]></dc:creator>
		<pubDate>Tue, 26 Nov 2024 14:00:00 +0000</pubDate>
				<category><![CDATA[Learning]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Advanced Logging]]></category>
		<category><![CDATA[Custom Classes]]></category>
		<category><![CDATA[Debugging Tools]]></category>
		<category><![CDATA[Diagnostics Tools]]></category>
		<category><![CDATA[Error Handling]]></category>
		<category><![CDATA[File Logging]]></category>
		<category><![CDATA[Log Rotation]]></category>
		<guid isPermaLink="false">https://blog.xojo.com/?p=14052</guid>

					<description><![CDATA[Logging is an essential part of software development. Whether you’re debugging issues during development or monitoring application performance in production, a robust logging solution is&#8230;]]></description>
										<content:encoded><![CDATA[
<p>Logging is an essential part of software development. Whether you’re debugging issues during development or monitoring application performance in production, a robust logging solution is critical. Xojo provides two excellent built-in methods for logging:&nbsp;<code>System.DebugLog</code>&nbsp;and&nbsp;<code>System.Log</code>. These methods are simple to use, highly effective, and suitable for most types of projects.</p>



<p>However, as your applications grow in complexity, you may encounter scenarios where more advanced logging features are required. Developers working with other programming languages often turn to frameworks like&nbsp;Log4J&nbsp;or&nbsp;Log4Net&nbsp;for flexible and powerful logging solutions. Inspired by these tools, let&#8217;s create&nbsp;Log4Xojo, a powerful and flexible logging utility designed specifically for the Xojo language.</p>



<p>With Log4Xojo, you can:</p>



<ul class="wp-block-list">
<li>Save logs to files for long-term storage and analysis.</li>



<li>Automatically rotate log files when they grow too large.</li>



<li>Log messages to multiple destinations (e.g., Debugger, system logs, and files) simultaneously.</li>



<li>Filter log messages by severity to reduce noise in production environments.</li>
</ul>



<p>Log4Xojo complements Xojo&#8217;s built-in logging methods by extending their capabilities and making it easier to manage logs in larger or more complex applications. Whether you&#8217;re a seasoned Xojo developer or coming from other tools like Log4J or Log4Net, Log4Xojo provides a familiar, robust solution for advanced logging needs.</p>



<h2 class="wp-block-heading"><strong>Xojo’s Built-In Logging Methods</strong></h2>



<p>Xojo provides two built-in methods for logging: <code>System.DebugLog</code> and <code>System.Log</code></p>



<ol class="wp-block-list">
<li><code>System.DebugLog</code> &#8211; <a href="https://documentation.xojo.com/api/os/system.html#system-debuglog" target="_blank" rel="noreferrer noopener">check here for the documentation</a></li>



<li><code>System.Log</code> &#8211; <a href="https://documentation.xojo.com/api/os/system.html#system-log" target="_blank" rel="noreferrer noopener">check here for the documentation</a></li>
</ol>



<p>These methods are simple to use and provide an effective way to track issues during development or monitor application behavior in production environments.</p>



<h3 class="wp-block-heading"><strong>1. System.DebugLog</strong></h3>



<p>The <code>System.DebugLog</code> method outputs messages to Xojo&#8217;s <strong>Message Log</strong> in the <a target="_blank" rel="noreferrer noopener" href="https://documentation.xojo.com/getting_started/using_the_ide/find-_errors-_messages_panels.html#getting-started-using-the-ide-find-errors-messages-panels-find"><strong>Messages Panel</strong></a> during development. This allows developers to generate their own logging messages to assist with debugging and testing. Additionally, <code>System.DebugLog</code> writes to the system log, making it viewable in tools such as:</p>



<ul class="wp-block-list">
<li><strong>Console</strong> on macOS</li>



<li><strong>DebugView</strong> on Windows</li>



<li><strong>StdErr</strong> on Linux</li>
</ul>



<h4 class="wp-block-heading">Example:</h4>



<pre class="wp-block-code"><code>System.DebugLog("This is a debug message.")</code></pre>



<h4 class="wp-block-heading">Key Features:</h4>



<ul class="wp-block-list">
<li>Outputs directly to the <strong>Messages Panel</strong> in the Xojo IDE during debugging, providing real-time insight into your application.</li>



<li>Also logs to the system log, which is accessible outside of the Xojo IDE for further analysis.</li>
</ul>



<h4 class="wp-block-heading">Limitations:</h4>



<ul class="wp-block-list">
<li>While <code>System.DebugLog</code> is versatile, its output cannot be directed to custom log files within your application without additional handling.</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading"><strong>2. System.Log</strong></h3>



<p>The <code>System.Log</code> method writes log messages to the system’s logging mechanism. This is particularly useful for monitoring application behavior in production environments, as it integrates seamlessly with system-level logging tools.</p>



<h4 class="wp-block-heading"><strong>How It Works</strong>:</h4>



<ul class="wp-block-list">
<li><strong>On macOS and Linux</strong>: Messages are written to the system logger, typically located at <code>/var/log</code>. These logs can be accessed using tools like <code>Console</code> (macOS) or <code>Syslog</code> (Linux).</li>



<li><strong>On Windows</strong>: Messages are sent to the Event Logger, which can be viewed using the Windows Event Viewer.</li>
</ul>



<h4 class="wp-block-heading">Example:</h4>



<pre class="wp-block-code"><code>System.Log(System.LogLevelNotice, "This is a notice message.")</code></pre>



<h4 class="wp-block-heading">Key Features:</h4>



<ul class="wp-block-list">
<li>Integrates with the system’s logging infrastructure, allowing log messages to persist outside of the Xojo IDE.</li>



<li>Useful for production environments, where logs can be reviewed using system tools like Console (macOS), Event Viewer (Windows), or Syslog (Linux).</li>
</ul>



<h4 class="wp-block-heading">Limitations:</h4>



<ul class="wp-block-list">
<li>Messages may not be portable across platforms due to system-specific log storage locations.</li>



<li>Not supported in mobile projects.</li>



<li>On <strong>macOS</strong>, messages logged with the following levels do not appear in the system log due to a macOS limitation:
<ul class="wp-block-list">
<li><code>LogLevelDebug</code></li>



<li><code>LogLevelInformation</code></li>



<li><code>LogLevelSuccess</code></li>
</ul>
</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading"><strong>Why Use the Built-In Methods?</strong></h3>



<p>The built-in logging methods are excellent for most projects. They are simple, efficient, and integrate seamlessly with Xojo. If your project doesn’t require file logging, log rotation, or multi-destination support, these methods might be all you need.</p>



<h2 class="wp-block-heading"><strong>Introducing our custom class: Log4Xojo</strong></h2>



<p>While Xojo’s built-in methods are excellent for many projects, some applications demand more advanced logging features. Log4Xojo is designed to complement Xojo’s built-in methods and extend their functionality to handle more complex requirements.</p>



<h3 class="wp-block-heading"><strong>What is Log4Xojo?</strong></h3>



<p>Log4Xojo is a custom-built logging class that expands Xojo’s logging capabilities with features like:</p>



<ul class="wp-block-list">
<li><strong>File Logging</strong>: Save logs to files for later analysis.</li>



<li><strong>Log Rotation</strong>: Automatically manage log file sizes and create backups.</li>



<li><strong>Multi-Destination Logging</strong>: Log messages to the Debugger, system logs, and files simultaneously.</li>



<li><strong>Log Level Filtering</strong>: Control which messages are logged based on severity (e.g., only log warnings and errors in production).</li>



<li><strong>Named Logs</strong>: Assign each log instance a unique name, making it easier to organize and differentiate log files.</li>



<li><strong>Thread-Safe Logging</strong>: Use a background thread to process logs efficiently without blocking your application.</li>
</ul>



<p>These features make Log4Xojo an excellent choice for larger or more complex projects, as well as applications with strict monitoring or reporting requirements.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading"><strong>When Should You Use Log4Xojo?</strong></h3>



<p>Consider using Log4Xojo if your project requires:</p>



<ul class="wp-block-list">
<li>Persistent logging to files for long-term storage or analysis.</li>



<li>Automatic log management to prevent files from growing too large.</li>



<li>Logging to multiple destinations simultaneously (e.g., Debugger, system logs, and files).</li>



<li>Filtering logs by severity to reduce noise in production environments.</li>



<li>Thread-safe, asynchronous logging for improved performance in multi-threaded applications. By processing logs asynchronously in a background thread, Log4Xojo ensures that your application’s performance remains smooth, even when handling a large volume of log messages.</li>
</ul>



<p>Log4Xojo is not meant to replace the built-in logging methods but to complement them. It builds on the foundation of <code>System.DebugLog</code> and <code>System.Log</code> by adding advanced features for developers who need more flexibility and control.</p>



<h2 class="wp-block-heading"><strong>How to Use Log4Xojo</strong></h2>



<p>Using Log4Xojo is simple! Follow these steps to get started.</p>



<h3 class="wp-block-heading"><strong>Step 1: Add Log4Xojo to Your Project</strong></h3>



<ol class="wp-block-list">
<li>Download the <code>Log4Xojo</code> class file from GitHub <a href="https://github.com/xojo/log4xojo" target="_blank" rel="noreferrer noopener">https://github.com/xojo/log4xojo</a> or <a href="https://drive.google.com/file/d/1huAoi2Fh1nAEiI6Y7--hZTd_RJEAjAUy/view?usp=sharing" target="_blank" rel="noreferrer noopener">click here to download the Xojo example project</a>.</li>



<li>Drag the <code>Log4Xojo</code> class file into your Xojo project.</li>
</ol>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading"><strong>Step 2: Create a Log Instance</strong></h3>



<p>Create an instance of the Log4Xojo class. Each log requires a name, which is used to identify it and organize log files.</p>



<pre class="wp-block-code"><code>Var l4x As New Log4Xojo("AppLog")</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading"><strong>Step 3: Configure the Log</strong></h3>



<p>Set up the logging destinations, file path, log level, and other settings. For example:</p>



<pre class="wp-block-code"><code>// Set destinations (Debugger and File)
l4x.SetLogDestinations(Log4Xojo.LogDestination.DebugLog, Log4Xojo.LogDestination.FileLog)

// Set the base folder for the log file
l4x.SetLogFilePath(SpecialFolder.Desktop)

// Set the log level to Warning and above
l4x.SetLogLevel(Log4Xojo.LogLevel.Warning)

// Configure log rotation
l4x.SetMaxBackupFiles(5)
l4x.SetMaxLogFileSize(1024)</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading"><strong>Step 4: Log Messages</strong></h3>



<p>Log messages with varying severity levels:</p>



<pre class="wp-block-code"><code>l4x.Log("Application started.", Log4Xojo.LogLevel.Info, CurrentMethodName)
l4x.Log("This is a debug message.", Log4Xojo.LogLevel.Debug, CurrentMethodName)
l4x.Log("Warning: Low disk space.", Log4Xojo.LogLevel.Warning, CurrentMethodName)
l4x.Log("Error: File not found.", Log4Xojo.LogLevel.Error, CurrentMethodName)</code></pre>



<p>The optional location parameter (<code>CurrentMethodName</code> above) allows you to include the context of the log message, such as the method or class where it originated. This makes it easier to pinpoint the source of issues when reviewing logs.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading"><strong>Step 5: Stop Logging</strong></h3>



<p>When your application shuts down, make sure to stop logging to ensure all queued messages are processed:</p>



<pre class="wp-block-code"><code>l4x.StopLogging()</code></pre>



<h2 class="wp-block-heading"><strong>Use Case Scenarios for Log4Xojo</strong></h2>



<p>To better understand how <code>Log4Xojo</code> can enhance your Xojo applications, here are some practical use cases:</p>



<h4 class="wp-block-heading"><strong>1. Monitoring Application Performance in Production</strong></h4>



<p>In a production environment, it’s essential to keep track of your application’s behavior without impacting performance. With <code>Log4Xojo</code>, you can:</p>



<ul class="wp-block-list">
<li>Log important application events (e.g., user activity, API calls).</li>



<li>Use <strong>file logging</strong> to store these logs persistently for later analysis.</li>



<li>Filter logs by severity to avoid unnecessary noise (e.g., only warnings, errors, and critical issues).</li>
</ul>



<p><strong>Example:</strong></p>



<pre class="wp-block-code"><code>Var l4x As New Log4Xojo("ProductionLog")
l4x.SetLogDestinations(Log4Xojo.LogDestination.FileLog)
l4x.SetLogLevel(Log4Xojo.LogLevel.Warning)

// Log application events
l4x.Log("User logged in", Log4Xojo.LogLevel.Info) // Ignored (below warning level)
l4x.Log("Database connection failed", Log4Xojo.LogLevel.Error) // Logged
l4x.Log("Critical: Payment gateway unreachable", Log4Xojo.LogLevel.Critical) // Logged</code></pre>



<p>Outcome: Only warnings, errors, and critical messages are logged to a file for postmortem analysis without overwhelming the log with lower-priority messages.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading"><strong>2. Creating a Diagnostic Tool for End Users</strong></h4>



<p>When troubleshooting issues reported by end users, having detailed logs can be invaluable. With <code>Log4Xojo</code>, you can:</p>



<ul class="wp-block-list">
<li>Log messages to a file on the user&#8217;s machine.</li>



<li>Include optional location tags to pinpoint where issues occur in your code.</li>



<li>Use <strong>log rotation</strong> to prevent log files from consuming too much disk space.</li>
</ul>



<p><strong>Example:</strong></p>



<pre class="wp-block-code"><code>Var l4x As New Log4Xojo("UserDiagnostics")
l4x.SetLogDestinations(Log4Xojo.LogDestination.FileLog)
l4x.SetLogFilePath(SpecialFolder.Documents)
l4x.SetMaxLogFileSize(1 * 1024 * 1024) // 1 MB
l4x.SetMaxBackupFiles(3)

// Log diagnostic information
l4x.Log("Application launched", Log4Xojo.LogLevel.Info, CurrentMethodName)
l4x.Log("Error: File not found", Log4Xojo.LogLevel.Error, "FileManager.LoadFile")
l4x.Log("User clicked 'Submit'", Log4Xojo.LogLevel.Debug, "MainWindow.HandleSubmit")</code></pre>



<p>Outcome: You can ask users to send the log files stored in their Documents folder for review, helping you quickly diagnose and fix issues.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading"><strong>3. Tracking User Activity in Enterprise Applications</strong></h4>



<p>In enterprise applications, logging user activity is often a requirement for auditing or compliance purposes. With <code>Log4Xojo</code>, you can:</p>



<ul class="wp-block-list">
<li>Use <strong>multi-destination logging</strong> to send activity logs to both the system logs and a central log file.</li>



<li>Include relevant context for each log entry (e.g., user ID, method).</li>
</ul>



<p><strong>Example:</strong></p>



<pre class="wp-block-code"><code>Var l4x As New Log4Xojo("AuditLog")
l4x.SetLogDestinations(Log4Xojo.LogDestination.SystemLog, Log4Xojo.LogDestination.FileLog)
l4x.SetLogFilePath(SpecialFolder.Documents)

// Log user activity
Var userID As String = "User123"
l4x.Log(userID + " logged in", Log4Xojo.LogLevel.Info, "AuthManager.Login")
l4x.Log(userID + " updated profile", Log4Xojo.LogLevel.Info, "ProfileManager.UpdateProfile")
l4x.Log(userID + " attempted unauthorized access", Log4Xojo.LogLevel.Warning, "SecurityManager.CheckPermissions")</code></pre>



<p>Outcome: Both system logs and a persistent file log are updated with the user&#8217;s activities, ensuring compliance and easy traceability.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading"><strong>4. Handling Errors and Crashes Gracefully</strong></h4>



<p>When an application crashes, logs are often the only way to understand what went wrong. <code>Log4Xojo</code> can:</p>



<ul class="wp-block-list">
<li>Capture error and critical logs leading up to a crash.</li>



<li>Rotate logs to avoid losing older, relevant logs.</li>



<li>Save logs to a file for recovery after a crash.</li>
</ul>



<p><strong>Example:</strong></p>



<pre class="wp-block-code"><code>Var l4x As New Log4Xojo("CrashLogs")
l4x.SetLogDestinations(Log4Xojo.LogDestination.FileLog)
l4x.SetMaxBackupFiles(5)
l4x.SetMaxLogFileSize(1 * 1024 * 1024) // 1 MB

Try
  // Simulate application logic
  Raise New RuntimeException("Simulated crash")
Catch e As RuntimeException
  l4x.Log("Critical error: " + e.Message, Log4Xojo.LogLevel.Critical, CurrentMethodName)
End Try</code></pre>



<p>Outcome: The log files can be used to investigate the cause of the crash.</p>



<h2 class="wp-block-heading"><strong>The Structure of the Log4Xojo Class</strong></h2>



<p>Log4Xojo includes several constants, enums, properties, and methods that provide its advanced functionality. Let’s break them down:</p>



<h3 class="wp-block-heading"><strong>Constants</strong></h3>



<ol class="wp-block-list">
<li><code>MaxQueueSize</code>
<ul class="wp-block-list">
<li>Defines the maximum number of log messages that can be stored in the logging queue before processing.</li>



<li>Default: <code>10,000</code>.</li>
</ul>
</li>
</ol>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading"><strong>Enums</strong></h3>



<ol class="wp-block-list">
<li><code>LogDestination</code>
<ul class="wp-block-list">
<li>Specifies where log messages should be sent:
<ul class="wp-block-list">
<li><code>DebugLog</code>: Logs to the Xojo Debugger using <code>System.DebugLog</code>.</li>



<li><code>SystemLog</code>: Logs to the system&#8217;s logging framework using <code>System.Log</code>.</li>



<li><code>FileLog</code>: Logs to a file.</li>



<li><code>All</code>: Logs to all destinations.</li>
</ul>
</li>
</ul>
</li>



<li><code>LogLevel</code>
<ul class="wp-block-list">
<li>Defines the severity levels for log messages:
<ul class="wp-block-list">
<li><code>Debug</code>: Detailed debugging information.</li>



<li><code>Info</code>: General informational messages.</li>



<li><code>Warning</code>: Potential issues that don’t interrupt program flow.</li>



<li><code>Error</code>: Errors requiring attention.</li>



<li><code>Critical</code>: Critical issues that may cause application failure.</li>
</ul>
</li>
</ul>
</li>
</ol>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading"><strong>Properties</strong></h3>



<ol class="wp-block-list">
<li><code>mLogName</code>
<ul class="wp-block-list">
<li>The name of the log. This is required during initialization and is used to identify logs and organize log files.</li>
</ul>
</li>



<li><code>mCurrentLogLevel</code>
<ul class="wp-block-list">
<li>The minimum severity level of messages to log. Messages below this level are ignored.</li>
</ul>
</li>



<li><code>mLogDestinations</code>
<ul class="wp-block-list">
<li>An array of <code>LogDestination</code> values specifying where logs should be sent.</li>
</ul>
</li>



<li><code>mLogFilePath</code>
<ul class="wp-block-list">
<li>The file path for logs. This is automatically generated based on the log name and current date but can also be customized.</li>
</ul>
</li>



<li><code>mMaxBackupFiles</code>
<ul class="wp-block-list">
<li>The maximum number of backup log files to retain. Default: <code>10</code>.</li>
</ul>
</li>



<li><code>mMaxLogFileSize</code>
<ul class="wp-block-list">
<li>The maximum size of the log file in bytes before rotation occurs. Default: <code>1 MB</code>.</li>
</ul>
</li>



<li><code>mLogQueue</code>
<ul class="wp-block-list">
<li>A queue for temporarily storing log messages before they are processed.</li>
</ul>
</li>



<li><code>mLogQueueMutex</code>
<ul class="wp-block-list">
<li>A mutex used to ensure thread safety when accessing the log queue. <a href="https://documentation.xojo.com/api/language/mutex.html#mutex" target="_blank" rel="noreferrer noopener">Check mutex documentation here</a>.</li>
</ul>
</li>



<li><code>mLogThread</code>
<ul class="wp-block-list">
<li>A background thread that processes log messages. <a href="https://documentation.xojo.com/api/language/thread.html#properties" target="_blank" rel="noreferrer noopener">Check thread documentation here</a>.</li>
</ul>
</li>



<li><code>mRunning</code>
<ul class="wp-block-list">
<li>A flag indicating whether the logging thread is running.</li>
</ul>
</li>
</ol>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading"><strong>Methods</strong></h3>



<p>The <code>Log4Xojo</code> class provides several methods to configure logging behavior and manage log messages. Let’s go through each one in detail:</p>



<h4 class="wp-block-heading"><strong>1. Constructor(Name As String)</strong></h4>



<p><strong>Purpose</strong>: The constructor initializes a new instance of the <code>Log4Xojo</code> class with a required log name. This name is used to identify the log instance and organize log files.</p>



<p><strong>Usage</strong>:</p>



<pre class="wp-block-code"><code>Var l4x As New Log4Xojo("AppLog")</code></pre>



<p><strong>Details</strong>:</p>



<ul class="wp-block-list">
<li>The <code>Name</code> parameter is required and cannot be empty. If you pass an empty string, the class will raise an <code>InvalidArgumentException</code>.</li>



<li>The log name is used to generate log file names (e.g., <code>AppLog_2024-11-01.txt</code>).</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading"><strong>2. Log(message As String, level As LogLevel, Optional location As String = &#8220;&#8221;)</strong></h4>



<p><strong>Purpose</strong>: Logs a message with a specified log level and an optional location string.</p>



<p><strong>Usage</strong>:</p>



<pre class="wp-block-code"><code>l4x.Log("This is an informational message.", Log4Xojo.LogLevel.Info)
l4x.Log("Warning: Low disk space.", Log4Xojo.LogLevel.Warning, CurrentMethodName)</code></pre>



<p><strong>Details</strong>:</p>



<ul class="wp-block-list">
<li><code>message</code>: The log message you want to record.</li>



<li><code>level</code>: The severity level of the message. This determines whether the message will be logged, based on the current log level (<code>mCurrentLogLevel</code>).</li>



<li><code>location</code> (optional): A string to provide context for the log message, such as the method or class where it originated. This is especially useful for tracing the source of issues. Using <code>CurrentMethodName</code> is more than enough, unless you need to specify something else.</li>



<li>If the log level is below the configured <code>mCurrentLogLevel</code>, the message is ignored.</li>
</ul>



<p><strong>Example Output</strong>:</p>



<pre class="wp-block-code"><code>&#91;2023-10-01 12:34:56] &#91;INFO] This is an informational message.
&#91;2023-10-01 12:35:00] &#91;WARNING] &#91;Code-Location] Warning: Low disk space.</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading"><strong>3. SetLogDestinations(ParamArray destinations As LogDestination)</strong></h4>



<p><strong>Purpose</strong>: Specifies where log messages should be sent (e.g., Debugger, system logs, files, or all destinations).</p>



<p><strong>Usage</strong>:</p>



<pre class="wp-block-code"><code>l4x.SetLogDestinations(Log4Xojo.LogDestination.DebugLog, Log4Xojo.LogDestination.FileLog)</code></pre>



<p><strong>Details</strong>:</p>



<ul class="wp-block-list">
<li>Accepts one or more <code>LogDestination</code> values as parameters.</li>



<li>Supported destinations:
<ul class="wp-block-list">
<li><code>LogDestination.DebugLog</code>: Logs to the Xojo Debugger using <code>System.DebugLog</code>.</li>



<li><code>LogDestination.SystemLog</code>: Logs to the system&#8217;s logging framework using <code>System.Log</code>.</li>



<li><code>LogDestination.FileLog</code>: Logs to a file.</li>



<li><code>LogDestination.All</code>: Logs to all destinations simultaneously.</li>
</ul>
</li>



<li>You can mix and match destinations to suit your needs.</li>
</ul>



<p><strong>Example</strong>:</p>



<ul class="wp-block-list">
<li>To log messages only to a file:<code>l4x.SetLogDestinations(Log4Xojo.LogDestination.FileLog)</code></li>



<li>To log messages to both the Debugger and system logs:<code>l4x.SetLogDestinations(Log4Xojo.LogDestination.DebugLog, Log4Xojo.LogDestination.SystemLog)</code></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading"><strong>4. SetLogFilePath(baseLocation As FolderItem)</strong></h4>



<p><strong>Purpose</strong>: Sets the folder where log files should be stored.</p>



<p><strong>Usage</strong>:</p>



<pre class="wp-block-code"><code>Var folder As FolderItem = SpecialFolder.Desktop
l4x.SetLogFilePath(folder)</code></pre>



<p><strong>Details</strong>:</p>



<ul class="wp-block-list">
<li><code>baseLocation</code>: A <code>FolderItem</code> representing the folder where log files will be saved.</li>



<li>The folder must exist and be writable. If the folder is invalid, the method will raise an <code>InvalidArgumentException</code>.</li>



<li>The log file name is automatically generated based on the log name and current date (e.g., <code>AppLog_2024-11-01.txt</code>).</li>
</ul>



<p><strong>Example</strong>:</p>



<ul class="wp-block-list">
<li>To store logs in the Documents folder:<code>l4x.SetLogFilePath(SpecialFolder.Documents)</code></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading"><strong>5. SetLogLevel(level As LogLevel)</strong></h4>



<p><strong>Purpose</strong>: Sets the minimum severity level for messages to be logged.</p>



<p><strong>Usage</strong>:</p>



<pre class="wp-block-code"><code>l4x.SetLogLevel(Log4Xojo.LogLevel.Warning)</code></pre>



<p><strong>Details</strong>:</p>



<ul class="wp-block-list">
<li><code>level</code>: A <code>LogLevel</code> value that determines which log messages will be recorded.</li>



<li>Messages with a severity lower than the set level are ignored.</li>
</ul>



<p><strong>Supported Log Levels</strong>:</p>



<ul class="wp-block-list">
<li><code>Log4Xojo.LogLevel.Debug</code>: Detailed debugging information.</li>



<li><code>Log4Xojo.LogLevel.Info</code>: General informational messages.</li>



<li><code>Log4Xojo.LogLevel.Warning</code>: Potential issues that don’t interrupt program flow.</li>



<li><code>Log4Xojo.LogLevel.Error</code>: Errors requiring attention.</li>



<li><code>Log4Xojo.LogLevel.Critical</code>: Critical issues that may cause application failure.</li>
</ul>



<p><strong>Example</strong>: If you set the log level to <code>LogLevel.Warning</code>, only warnings, errors, and critical messages will be logged:</p>



<pre class="wp-block-code"><code>l4x.SetLogLevel(Log4Xojo.LogLevel.Warning)
l4x.Log("This is a debug message.", Log4Xojo.LogLevel.Debug) // Ignored
l4x.Log("This is a warning message.", Log4Xojo.LogLevel.Warning) // Logged</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading"><strong>6. SetMaxBackupFiles(max As Integer)</strong></h4>



<p><strong>Purpose</strong>: Configures the maximum number of backup log files to retain.</p>



<p><strong>Usage</strong>:</p>



<pre class="wp-block-code"><code>l4x.SetMaxBackupFiles(5)</code></pre>



<p><strong>Details</strong>:</p>



<ul class="wp-block-list">
<li><code>max</code>: The maximum number of backup files to keep. Older backups are automatically deleted when the limit is reached.</li>



<li>When the current log file exceeds the size limit (set via <code>SetMaxLogFileSize</code>), it is renamed as a backup (e.g., <code>AppLog_Date_1.txt</code>), and a new log file is created.</li>
</ul>



<p><strong>Example</strong>: If <code>max = 5</code>, the backups will look like this:</p>



<pre class="wp-block-code"><code>AppLog_Date_1.txt
AppLog_Date_2.txt
AppLog_Date_3.txt
AppLog_Date_4.txt
AppLog_Date_5.txt</code></pre>



<p>Once the 6th backup is created, <code>AppLog_Date_1.txt</code> is deleted to make room for it.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading"><strong>7. SetMaxLogFileSize(sizeInBytes As Integer)</strong></h4>



<p><strong>Purpose</strong>: Defines the maximum size of the log file before it is rotated.</p>



<p><strong>Usage</strong>:</p>



<pre class="wp-block-code"><code>l4x.SetMaxLogFileSize(1 * 1024 * 1024) // 1 MB</code></pre>



<p><strong>Details</strong>:</p>



<ul class="wp-block-list">
<li><code>sizeInBytes</code>: The maximum size of the log file in bytes.</li>



<li>When the log file exceeds this size, it is renamed as a backup, and a new log file is created.</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading"><strong>8. StopLogging()</strong></h4>



<p><strong>Purpose</strong>: Stops the logging thread and processes any remaining messages in the queue.</p>



<p><strong>Usage</strong>:</p>



<pre class="wp-block-code"><code>l4x.StopLogging()</code></pre>



<p><strong>Details</strong>:</p>



<ul class="wp-block-list">
<li>Use this method when your application is shutting down to ensure that all log messages are written to their destinations.</li>



<li>The method waits for a short period to process any remaining messages in the queue before terminating the logging thread.</li>
</ul>



<h2 class="wp-block-heading"><strong>Conclusion</strong></h2>



<p>Xojo’s built-in logging methods, <code>System.DebugLog</code> and <code>System.Log</code>, are excellent built-in tools for debugging and monitoring application behavior. They are simple, effective, and suitable for most types of projects.</p>



<p>For applications with more advanced requirements, the Log4Xojo class provides a powerful complement to these methods. With features like file logging, log rotation, multi-destination support, and log level filtering, Log4Xojo is the perfect solution for larger or more complex projects.</p>



<p class="has-text-align-center">Download Log4Xojo from GitHub <a href="https://github.com/xojo/log4xojo" target="_blank" rel="noreferrer noopener">https://github.com/xojo/log4xojo</a> or as a <a href="https://drive.google.com/file/d/1huAoi2Fh1nAEiI6Y7--hZTd_RJEAjAUy/view?usp=sharing" data-type="link" data-id="https://drive.google.com/file/d/1huAoi2Fh1nAEiI6Y7--hZTd_RJEAjAUy/view?usp=sharing" target="_blank" rel="noreferrer noopener">Xojo Binary Project</a> file and start enhancing your logging workflow.</p>



<p>We look forward to hearing how you’re using Log4Xojo in your projects!</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
