gotree

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 23, 2023 License: MIT Imports: 3 Imported by: 0

README

Go-tree

Go Reference Go Report Card GitHub go.mod Go version License

preview

Overview

Go-tree is a Go package that provides an intuitive and visually structured logging solution for Go applications. It allows developers to create hierarchical log messages that are displayed in an organized, tree-like format. This structure is ideal for debugging the execution flow of complex applications, and where understanding the relationship between various log messages is crucial.

Features

  • Execution Flow Mapping: Designed for tracing the intricate execution paths in modern Go applications.
  • Enhanced Readability: Say goodbye to cluttered logs. Go-tree brings clarity to your log data by displaying log messages in a nested tree structure.
  • Multiple Log Levels: Supports adding various log levels to the tree such as info, debug, error, and warn.
  • Easy to Use: Designed for simplicity, allowing you to integrate structured logging with minimal setup.
  • Flexible: Add plain or formatted messages at any level of the tree.
  • Console Friendly: Designed for console output, making it perfect for development and debugging.

Installation

Install go-tree in your package by running:

go get github.com/zilayo/go-tree/

Usage

Firstly create a new tree: tree, parentIndex := gotree.NewTree("Example Tree")

The NewTree, AddMessage, AddInfo, AddDebug, AddWarn, AddError, and AddMessages functions all return an int which represents the index of the newly created node within the tree.

The index returned by NewTree represents the outermost node of the tree.

To add a branch to a specific node, you must the node's index within the Add functions.

All branches are ended with └─ ←, making it easy to understand where a branch ends.

// Adds nodes directly to the outermost node of the tree (parentIndex)
tree.AddMessage(parentIndex, "Message to add")
tree.AddInfo(parentIndex, "Info message to add")
tree.AddDebug(parentIndex, "Debug message to add")
tree.AddWarn(parentIndex, "Warn message to add")
tree.AddError(parentIndex, "Error message to add")
tree.AddMessages(parentIndex, []string{"First message to add", "Second message to add"})

// Adds a node to the outermost node of the tree. Assigns a reference to the index of this new node, so that a new branch can be created.
branchIndex := tree.AddInfo(parentIndex, "This node will have children")
tree.AddMessages(branchIndex, []string{"Branch child 1", "Branch child 2"})

Output:

  Example Tree
    ├─ Message to add
    ├─ info: Info message to add
    ├─ debug: Debug message to add
    ├─ warn: Warn message to add
    ├─ error: Error message to add
    ├─ First message to add
    ├─ Second message to add
    ├─ info: This node will have children
    │   ├─ Branch child 1
    │   ├─ Branch child 2
    │   └─ ←
    └─ ←

Here is a basic example of how Go-tree can be used in an application. More examples can be found in gotree_test.go

package main

import "github.com/zilayo/go-tree/gotree"

func main(){
  // Name your tree. This will be displayed at the top of the tree when printed.
  tree, parentIndex := gotree.NewTree("Example Tree")

	// Add an info message
	tree.AddInfo(parentIndex, "I am an info branch!")

	// Add some regular messages
	tree.AddMessages(parentIndex, []string{"Or we can use plain messages!", "And more plain messages"})

	// Create a debug branch, get it's index, and create another node for this branch.
	debugIdx := tree.AddDebug(parentIndex, "I am a debug branch!")

	// Add a message to the debug branch
	tree.AddMessage(debugIdx, "plain messages can be added to info/debug/error/warn branches too")

	// Print the tree to standard out
	tree.Display()
}

Output:

  Example Tree
    ├─ info: I am an info branch!
    ├─ Or we can use plain messages!
    ├─ And more plain messages
    ├─ debug: I am a debug branch!
    │   ├─ plain messages can be added to info/debug/error/warn branches too
    │   └─ ←
    └─ ←

Contributing

Contributions are welcome! Feel free to submit issues or pull requests on the GitHub repository.

Acknowledgements

Go-tree is HEAVILY inspired by Jonathan Becker's Logging module used in heimdall-rs. Go-tree is essentially a minimized Go fork of trace & trace_factory in heimdall_common::utils::io::logging.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Node

type Node struct {
	// Category is the type of node being added.
	//  - Messages: a slice of messages to add to the node
	//  - Empty: a line break to add to the node.
	//  - Parent: the very top level node of the tree.
	Category NodeCategory
	// contains filtered or unexported fields
}

Node represents a single node of the tree

func NewNode

func NewNode(category NodeCategory, parentIndex int, message []string) Node

NewNode creates a new node with the given parameters

  • category: the type of node (Parent, Messages, or Empty)
  • parentIndex: the index of the parent node that this new node will belong to
  • messages: a slice of messages to display

type NodeCategory

type NodeCategory int

NodeCategory represents which type of node should be added to the tree

  • Messages: 0
  • Line break: 1
  • Parent: 2
const (
	// Messages represents a node with 1 or more message
	Messages NodeCategory = iota

	// Empty represents an empty node, which is used for line breaks
	Empty

	// Parent represents the tree's outer node.
	Parent
)

type Tree

type Tree struct {
	// Nodes is a slice containing every Node in the tree.
	Nodes []*Node
}

Tree represents the full tree, containing all of the child nodes.

func NewTree

func NewTree(treeName string) (*Tree, int)

NewNode creates a new Tree. Returns an instance of the tree, and the index of the parent node

func (*Tree) Add

func (t *Tree) Add(category NodeCategory, parentIndex int, messages []string) int

Add is a low level function to create and appends a node to the tree. Returns the index of the new node.

  • category: the type of node to add (Parent, Messages, or Empty)
  • parentIndex: the index of the parent node that this new node will belong to
  • message: the message to display.

func (*Tree) AddBreak

func (t *Tree) AddBreak(parentIndex int) int

AddBreak adds a line break to the tree

  • parentIndex: the index of the parent node that this new node will belong to

func (*Tree) AddDebug

func (t *Tree) AddDebug(parentIndex int, message string) int

AddDebug adds a debug message to the tree. Returns the index of the new tree.

  • parentIndex: the index of the parent node that this new node will belong to
  • message: the message to display.

func (*Tree) AddError

func (t *Tree) AddError(parentIndex int, message string) int

AddError adds an error message to the tree. Returns the index of the new tree.

  • parentIndex: the index of the parent node that this new node will belong to
  • message: the message to display.

func (*Tree) AddInfo

func (t *Tree) AddInfo(parentIndex int, message string) int

AddInfo adds an info message to the tree. Returns the index of the new tree.

  • parentIndex: the index of the parent node that this new node will belong to
  • message: the message to display.

func (*Tree) AddMessage

func (t *Tree) AddMessage(parentIndex int, message string) int

AddMessage adds a single message to the tree

  • parentIndex: the index of the parent node that this new node will belong to
  • message: the message to display.

func (*Tree) AddMessages

func (t *Tree) AddMessages(parentIndex int, messages []string) int

AddMessages adds a slice of messages to the tree

  • parentIndex: the index of the parent node that this new node will belong to
  • messages: a slice of messages to display.

func (*Tree) AddParent

func (t *Tree) AddParent(parentIndex int, message string) int

AddParent creates and adds a Parent node to the tree.

  • parentIndex: the index of the parent node that this new node will belong to
  • message: the message to display.

func (*Tree) AddWarn

func (t *Tree) AddWarn(parentIndex int, message string) int

AddWarn adds a warn message to the tree. Returns the index of the new tree.

  • parentIndex: the index of the parent node that this new node will belong to
  • message: the message to display.

func (Tree) Display

func (t Tree) Display()

Display will print the tree to standard output.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL