Skip to main content
In this guide, we will build a simple WebAssembly log extractor using Rust, WebAssembly, and JavaScript. By leveraging the powerful wasm-bindgen crate, this project demonstrates how to integrate Rust with JavaScript, enabling efficient log extraction operations on the web. Our objective is to create a web application that parses a log message formatted like this:
The application will extract the timestamp, severity, and message components, displaying them on the webpage. It will also handle invalid log formats gracefully by showing a clear error message.
Ensure that WASM pack is installed before starting this project.

Setting Up the Rust Library Project

Begin by creating a new Rust library project. Open your terminal and run:
Then, open the project in your preferred IDE, such as Visual Studio Code.

Configuring Cargo.toml

Edit your Cargo.toml file to include the necessary dependencies for wasm-bindgen, Serde, and serde-wasm-bindgen. These dependencies facilitate serialization and deserialization between Rust and JavaScript:

Writing the Rust Code

Open the main library file (typically src/lib.rs) and include the required imports:

Creating the LogEntry Struct

Define a public struct LogEntry that models the log components. The struct derives the Serialize and Deserialize traits to simplify conversion between Rust and JavaScript objects.

Implementing a Simple Function for Testing

Implement a simple addition function for demonstration purposes. This also includes a unit test to verify the functionality.

Writing the Log Extraction Function

Create the extract_log function that is exposed to JavaScript via the #[wasm_bindgen] attribute. This function splits the input log string into three components and returns a JavaScript value.
In this function, if the log is improperly formatted (fewer than three parts after splitting), an error message is returned. In a production environment, consider handling errors more robustly instead of using unwrap().

Compiling the Rust Code to WebAssembly

Compile your project to generate the WebAssembly binary along with the JavaScript binding file and package.json for npm integration. Ensure the Cargo.toml file includes the [lib] crate-type fields as shown above. When compiled, the output should resemble:
This step produces the WASM binary and the associated files required for further development.
For more advanced projects, consider adding descriptive fields like ‘description’, ‘repository’, and ‘license’ in the Cargo.toml.

Creating the Web Interface

Below is the complete HTML file that provides a user interface for the log extractor. The HTML includes basic CSS styling and JavaScript code to initialize and interact with the WebAssembly module.
The image shows a webpage titled "WebAssembly Log Extractor" with a form to enter a log message. It displays an extracted log with a timestamp, severity, and message.
In the HTML code above:
  • A loading indicator is shown while the WASM module loads.
  • Once initialized, the module is ready to process log entries.
  • When the “Extract Log” button is clicked, the extract_log function processes the input and displays the parsed output or an error message accordingly.

Testing the Application

To test the application, serve the HTML file using a live server extension or your preferred local server. When the page loads, the WASM module initializes. Enter a log message in the specified format (e.g., “2024-11-22 INFO System started”) and click the “Extract Log” button to display the extracted timestamp, severity, and message. For improperly formatted log messages, the error “Invalid log format” will be shown.

Conclusion

This tutorial has demonstrated how to build a WebAssembly-powered log extractor using Rust. We accomplished the following:
  • Created a Rust library with wasm-bindgen to interface with JavaScript.
  • Defined a LogEntry struct to model log data.
  • Implemented a function to extract and parse log entries.
  • Compiled the project to WebAssembly and integrated it with a web interface.
  • Tested the application with valid and invalid log formats.
This project highlights the flexibility and performance benefits of integrating Rust with WebAssembly, paving the way for building high-performance web applications. Happy coding!

Watch Video