DEV Community

Serge Artishev
Serge Artishev

Posted on

Advanced SQL Server: Upgrading Database Audit Logs with Loggly API Integration

In the constantly evolving landscape of database administration, innovative logging solutions are not just a luxury, but a necessity. It's time to revolutionize your database logging by integrating SQL Server with potent external logging services like Loggly. Let us introduce you to a powerful method that brings dynamism and real-time tracking to your database transactions - enter the SQL Server OLE Automation Procedures. This technique not only enhances your logging capabilities but brings a fresh perspective to database monitoring. Let's dive in!

Bridging the Gap: A Method that Embodies Autonomy

In many database systems, autonomous transactions allow changes to be committed independently of the main transaction. Unfortunately, MS SQL Server doesn't natively support this feature, which can sometimes limit its capabilities in scenarios like storing audit logs irrespective of the core transaction outcome.

However, this is where our method comes into play as a beacon of autonomy. By utilizing the OLE Automation Procedures to communicate with external logging services like Loggly, we can create an independent logging mechanism that works seamlessly, regardless of whether the primary transaction commits or rolls back. It’s an elegant solution that makes MS SQL Server stand toe-to-toe with other databases that support autonomous transactions.

Setting Up Your SQL Server for Success

Before you embark on this transformative journey, you must pave the way by setting up your SQL Server to allow OLE Automation Procedures. By default, this function is disabled to safeguard against potential security breaches. But fret not, enabling it is a walk in the park with the following script:

USE master;
GO

EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
GO

EXEC sp_configure 'Ole Automation Procedures', 1;
RECONFIGURE;
GO

Enter fullscreen mode Exit fullscreen mode

Remember, with great power comes great responsibility! Make sure to evaluate the potential risks and adhere to your organization's security protocols.

Crafting the Ultimate Stored Procedure: SendAuditLog

Now onto the magic potion – creating a stored procedure that sends your transaction logs to the sophisticated Loggly service seamlessly. Behold the SendAuditLog stored procedure that's about to change the game:

CREATE OR ALTER PROCEDURE dbo.SendAuditLog
  @ModuleName NVARCHAR(50),
  @Script NVARCHAR(MAX),
  @Date DATETIME,
  @Change NVARCHAR(50),
  @BeforeValue NVARCHAR(MAX),
  @AfterValue NVARCHAR(MAX)
AS
BEGIN
  DECLARE @jsonMessage NVARCHAR(MAX)
  SET @jsonMessage = '{' +
                     '"ModuleName": "' + ISNULL(@ModuleName, '') + '", ' +
                     '"Script": "' + ISNULL(@Script, '') + '", ' +
                     '"Date": "' + ISNULL(CONVERT(NVARCHAR, @Date, 126), CONVERT(NVARCHAR, GETDATE(), 126)) + '", ' +
                     '"Change": "' + ISNULL(@Change, '') + '", ' +
                     '"BeforeValue": "' + ISNULL(@BeforeValue, '') + '", ' +
                     '"AfterValue": "' + ISNULL(@AfterValue, '') + '"}'

  DECLARE @obj INT
  EXEC sp_OACreate 'MSXML2.ServerXMLHTTP', @obj OUT
  EXEC sp_OAMethod @obj, 'open', NULL, 'POST', 'http://logs-01.loggly.com/inputs/YOUR_API_KEY/tag/http/', 'false'
  EXEC sp_OAMethod @obj, 'setRequestHeader', NULL, 'Content-Type', 'application/json'
  EXEC sp_OAMethod @obj, 'send', NULL, @jsonMessage

  DECLARE @status INT
  EXEC sp_OAGetProperty @obj, 'status', @status OUT
  PRINT 'HTTP Status: ' + CAST(@status AS VARCHAR)

  EXEC sp_OADestroy @obj
END;

Enter fullscreen mode Exit fullscreen mode

Getting Acquainted with Loggly

Imagine having a platform that not only stores your log data but also provides insightful analytics and real-time monitoring. That's exactly what Loggly brings to the table. As a cloud-based log management and analytics service, Loggly transforms the way you visualize and analyze data, making it an enticing choice for organizations looking to elevate their logging strategy to the next level.

Usage Sample

Embarking on this new venture is as simple as executing the stored procedure with the necessary parameters. Here’s a snapshot of how you can use this procedure:

EXEC dbo.SendAuditLog 
  @ModuleName = 'UserModule',
  @Script = 'UserCreationScript',
  @Date = GETDATE(),
  @Change = 'User Details Modification',
  @BeforeValue = 'John Doe',
  @AfterValue = 'Jonathan Doe';

Enter fullscreen mode Exit fullscreen mode

Beyond Audit Logs: Exploring the Boundless Possibilities

While collecting audit logs is a quintessential use case, the sky is the limit when it comes to leveraging this procedure. Here are some other tantalizing opportunities:

  1. Real-Time Error Reporting: Harness the power of real-time error reporting, where you can catch and log errors as they happen, allowing for swift interventions.
  2. Data Integration: Seamlessly integrate data across various platforms, creating a unified data ecosystem that breathes efficiency.
  3. User Behavior Analysis: Capture and analyze user interactions with your database, paving the way for enhanced user experiences and service optimization.

Conclusion: Stepping Into a New Era of Database Logging

By integrating SQL Server with a robust logging service like Loggly, you're not just enhancing your logging capabilities; you're taking a bold step into a new era of database administration. It's time to leave the traditional methods behind and embrace a solution that's not only efficient but downright exhilarating. So, what are you waiting for? Dive in, and let the magic unfold!

Top comments (0)