tasklog

package module
v0.0.0-...-37cc870 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2025 License: MIT Imports: 6 Imported by: 0

README

TaskLog

Go Report Card Go Reference GitHub License Test

TaskLog is a task-based logger for Go (golang) with a simple API interface.

It's intended to be used by humans, as it uses ansi escape codes to format the output, making it easy to read in a terminal.

Installation

go get -u github.com/mrmarble/tasklog

Getting Started

Simple Logging Example

For simple logging, import the global logger package github.com/mrmarble/tasklog/log

package main

import "github.com/mrmarble/tasklog/log"

func main() {
    log.Task("Doing some work")
    log.Task("Another task")
    log.Skip("some reason")
}

// Output: [✓]  Doing some work
//         [→]  Another task (some reason)

Note: By default log writes to os.Stderr

Logging additional information

You can also log additional information by using the Printmethods

package main

import "github.com/mrmarble/tasklog/log"

func main() {
    log.Task("Doing some work")
    log.Print("This is some additional information")
}

// Output: [✓]  Doing some work
//              This is some additional information

By default, the additional information is deleted when a new task is started. You can change this behavior by using a custom logger.

Custom Logger

You can customize the logger by using the corresponding Setmethods.

package main

import (
    "os"

    "github.com/charmbracelet/x/ansi"
    "github.com/mrmarble/tasklog"
)

func main() {
    log := tasklog.New(os.Stdout)

    log.SetPersistent(true) // Keep the additional information when a new task is started
    log.SetProgressColor(ansi.HexColor("#88AAFF")). // Supports any color implementing color.Color interface

    log.Task("Doing some work")
}

// Output: [ ]  Doing some work

Contributing

I welcome any contributions. Just create a PR or an issue.

Documentation

Overview

Package tasklog provides a simple task logging system with support for task management, error handling, and output formatting.

A global Logger can be use for simple logging:

import "github.com/mrmarble/tasklog/log"

log.Task("hello world")
log.Done()
// Output: [✓] hello world

NOTE: To import the global logger, import the "log" subpackage "github.com/mrmarble/tasklog/log".

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TaskLogger

type TaskLogger struct {
	// contains filtered or unexported fields
}

A TaskLogger represents an active logging object that can manage tasks, errors, and output formatting.

func New

func New(w io.Writer) *TaskLogger

New creates a new TaskLogger instance with the specified output writer.

Example
package main

import (
	"os"

	"github.com/mrmarble/tasklog"
)

func main() {
	log := tasklog.New(os.Stdout)
	log.Task("Example Task")
}
Output:

�[1;30m [ ]  Example Task�[m

func (*TaskLogger) Clear

func (t *TaskLogger) Clear()

Clear clears the current task context.

func (*TaskLogger) Done

func (t *TaskLogger) Done()

Done marks the current task as completed.

func (*TaskLogger) Error

func (t *TaskLogger) Error(e error)

Error marks the current task as failed with an error.

func (*TaskLogger) Print

func (t *TaskLogger) Print(args ...any)

Print prints a message to the logger without a newline, prepending a tab for alignment.

func (*TaskLogger) Printf

func (t *TaskLogger) Printf(format string, args ...any)

Printf formats and prints a message to the logger, prepending a tab for alignment.

func (*TaskLogger) Println

func (t *TaskLogger) Println(args ...any)

Println prints a message to the logger with a newline, prepending a tab for alignment.

func (*TaskLogger) SetErrorColor

func (t *TaskLogger) SetErrorColor(c color.Color)

SetErrorColor sets the color for error messages.

func (*TaskLogger) SetPersistError

func (t *TaskLogger) SetPersistError(persistError bool)

SetPersistError sets whether the error should remain in context after completion.

func (*TaskLogger) SetPersistent

func (t *TaskLogger) SetPersistent(persistent bool)

SetPersistent sets whether the task should remain in context after completion.

func (*TaskLogger) SetProgressColor

func (t *TaskLogger) SetProgressColor(c color.Color)

SetProgressColor sets the color for the active task.

func (*TaskLogger) SetSkipColor

func (t *TaskLogger) SetSkipColor(c color.Color)

SetSkipColor sets the color for skipped tasks.

Example
package main

import (
	"os"

	"github.com/charmbracelet/x/ansi"
	"github.com/mrmarble/tasklog"
)

func main() {
	log := tasklog.New(os.Stdout)
	log.SetSkipColor(ansi.BrightCyan)
	log.Task("Example Task")
	log.Skip()

}
Output:

�[1;30m [ ]  Example Task�[m
�[2K�[A�[1G�[2K�[1;96m [→]  Example Task�[m

func (*TaskLogger) SetSuccessColor

func (t *TaskLogger) SetSuccessColor(c color.Color)

SetSuccessColor sets the color for success messages.

Example
package main

import (
	"image/color"
	"os"

	"github.com/mrmarble/tasklog"
)

func main() {
	log := tasklog.New(os.Stdout)
	log.SetSuccessColor(color.RGBA{R: 0, G: 255, B: 0, A: 255})
	log.Task("Example Task")
	log.Done()
}
Output:

�[1;30m [ ]  Example Task�[m
�[2K�[A�[1G�[2K�[1;38;2;0;255;0m [✓]  Example Task�[m

func (*TaskLogger) Skip

func (t *TaskLogger) Skip(reason ...string)

Skip marks the current task as skipped with an optional reason.

func (*TaskLogger) Task

func (t *TaskLogger) Task(name string)

Task creates a new task with the given name.

Directories

Path Synopsis
Simple cli example of tasklog usage.
Simple cli example of tasklog usage.
Package log provides a global logger for tasklog.
Package log provides a global logger for tasklog.

Jump to

Keyboard shortcuts

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