• Stars
    star
    92
  • Rank 361,527 (Top 8 %)
  • Language
    R
  • Created about 14 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A log4j derivative for R.

log4r

CRAN status R-CMD-check

log4r is a fast, lightweight, object-oriented approach to logging in R based on the widely-emulated Apache Log4j project.

log4r differs from other R logging packages in its focus on performance and simplicity. As such, it has fewer features – although it is still quite extensible, as seen below – but is much faster. See vignette("performance", package = "log4r") for details.

Unlike other R logging packages, log4r also has first-class support for structured logging. See vignette("structured-logging", package = "log4r") for details.

Installation

The package is available from CRAN:

install.packages("log4r")

If you want to use the development version, you can install the package from GitHub as follows:

# install.packages("remotes")
remotes::install_github("johnmyleswhite/log4r")

Usage

Logging is configured by passing around logger objects created by logger(). By default, this will log to the console and suppress messages below the "INFO" level:

logger <- logger()

info(logger, "Located nearest gas station.")
#> INFO  [2019-09-04 16:31:04] Located nearest gas station.
warn(logger, "Ez-Gas sensor network is not available.")
#> WARN  [2019-09-04 16:31:04] Ez-Gas sensor network is not available.
debug(logger, "Debug messages are suppressed by default.")

Logging destinations are controlled by Appenders, a few of which are provided by the package. For instance, if we want to debug-level messages to a file:

log_file <- tempfile()
logger <- logger("DEBUG", appenders = file_appender(log_file))

info(logger, "Messages are now written to the file instead.")
debug(logger, "Debug messages are now visible.")

readLines(log_file)
#> [1] "INFO  [2019-09-04 16:31:04] Messages are now written to the file instead."
#> [2] "DEBUG [2019-09-04 16:31:04] Debug messages are now visible."

The appenders parameter takes a list, so you can log to multiple destinations transparently.

For local development or simple batch R scripts run manually, writing log messages to a file for later inspection is convenient. However, for deployed R applications or automated scripts it is more likely you will need to send logs to a central location; see vignette("logging-beyond-local-files", package = "log4r").

To control the format of the messages you can change the Layout used by each appender. Layouts are functions; you can write your own quite easily:

my_layout <- function(level, ...) {
  paste0(format(Sys.time()), " [", level, "] ", ..., collapse = "")
}

logger <- logger(appenders = console_appender(my_layout))
info(logger, "Messages should now look a little different.")
#> 2019-09-04 16:31:04 [INFO] Messages should now look a little different.

With an appropriate layout, you can also use structured logging, enriching log messages with contextual fields:

logger <- logger(appenders = console_appender(logfmt_log_layout()))
info(
  logger, message = "processed entries", file = "catpics_01.csv",
  entries = 4124, elapsed = 2.311
)
#> level=INFO ts=2021-10-22T20:19:21Z message="processed entries" file=catpics_01.csv entries=4124 elapsed=2.311

Older APIs

The 0.2 API is still supported:

logger <- create.logger()

logfile(logger) <- log_file
level(logger) <- "INFO"

debug(logger, 'A Debugging Message')
info(logger, 'An Info Message')
warn(logger, 'A Warning Message')
error(logger, 'An Error Message')
fatal(logger, 'A Fatal Error Message')

readLines(log_file)
#> [1] "INFO  [2019-09-04 16:31:05] An Info Message"      
#> [2] "WARN  [2019-09-04 16:31:05] A Warning Message"    
#> [3] "ERROR [2019-09-04 16:31:05] An Error Message"     
#> [4] "FATAL [2019-09-04 16:31:05] A Fatal Error Message"

License

The package is available under the terms of the Artistic License 2.0.

More Repositories

1

ML_for_Hackers

Code accompanying the book "Machine Learning for Hackers"
R
3,650
star
2

BanditsBook

Code for my book on Multi-Armed Bandit Algorithms
R
887
star
3

MLNotes

Very concise notes on machine learning and statistics.
R
379
star
4

SimpleAintEasy

A compendium of the pitfalls and problems that arise when using standard statistical methods
R
243
star
5

julia_tutorials

Tutorials on Julia topics
Jupyter Notebook
197
star
6

JAGSExamples

Examples of statistical models implemented using JAGS
R
97
star
7

Vega.jl

A Julia package for generating visualizations in Vega
Julia
84
star
8

r_recommendation_system

Data and code for the Dataists R recommendation system contest
R
75
star
9

Style.jl

Style guideline for Julia
74
star
10

JuliaVsR

Comparisons between Julia and R
HTML
58
star
11

bayesian_nonparametrics

Slides and code for Bayesian nonparametrics in R and Julia
R
53
star
12

StreamStats.jl

Compute statistics over data streams in pure Julia
Julia
48
star
13

Benchmarks.jl

A new benchmarking library for Julia
Julia
45
star
14

ASCIIPlots.jl

Generate simple plots as ASCII art in Julia
Julia
38
star
15

TextRegression

An R package that automates text regression analysis.
R
31
star
16

BehavioralEconomics

R package for behavioral economics
R
31
star
17

MNIST.jl

Tools for working with the MNIST data set
Julia
25
star
18

Volcanito.jl

A backend agnostic for tabular data operations in Julia
Julia
25
star
19

kNN.jl

The k-nearest neighbors algorithm in Julia
Julia
22
star
20

r-ORM

An ORM library for R.
19
star
21

Benchmark.jl

A package for benchmarking code and packages
Julia
18
star
22

BloomFilters.jl

Bloom filters in Julia
Julia
18
star
23

room_temperatures

Dataset of temperatures in 6 rooms of my home recorded once per minute
R
16
star
24

StatsFunctionsNotes

Jupyter notebooks showing to implement statistical functions.
Jupyter Notebook
14
star
25

HopfieldNets.jl

Hopfield networks in Julia
Julia
14
star
26

r_squared

Code for blog post on r-squared
R
13
star
27

ContinuedFractions.jl

Types and functions for working with continued fractions in Julia
Julia
12
star
28

Ranking.jl

Tools for ranking in Julia
Julia
11
star
29

CorruptCSVs

Examples of corrupt CSV files and how they trick various parsers
R
10
star
30

julia-function-docs

Repo for materials for coordinating work on improving Julia's function documentation
10
star
31

cumplyr

An extension of plyr that uses inequality constraints.
R
9
star
32

titlecase-itunes-tracks

A utility to rename the tracks in an iTunes library so that the artist, album and track name are all in proper English title case.
9
star
33

MySQL.jl

MySQL DBI driver
Julia
9
star
34

sudoku.jl

A simple Sudoku solver in Julia
Julia
8
star
35

IntertemporalChoiceHeuristics

Analysis pipeline for Psychological Science paper called "Money Earlier or Later? Simple Heuristics Explain Intertemporal Choices Better than Delay Discounting"
R
8
star
36

DataFramesIO.jl

Advanced import/export tools for DataFrames: Stata, SPSS, Excel, JSON
Julia
8
star
37

DG2012Tutorial.jl

Simple examples of SGD-style computations in Julia
Julia
8
star
38

computer_music

R functions for generating computer music.
R
8
star
39

Brainfuck.jl

A Brainfuck interpreter written in Julia
Julia
7
star
40

Resampling.jl

Tools for resampling data in Julia
Julia
7
star
41

analytics_x_prize

Code and data used for submissions to the Analytics X Prize
Python
7
star
42

IsingModels.jl

The Ising model as a Julian distribution
Julia
7
star
43

PowerAnalysis.jl

Tools for power analysis in Julia
Julia
7
star
44

spatial_statistics_in_r

Slides and code on spatial statistics in R.
7
star
45

FFIExamples.jl

Simple examples of Julia's FFI as of v0.4
Julia
7
star
46

HackerSchoolTalk

A talk to the Summer 2013 batch of Hacker School students
Ruby
7
star
47

KLDivergence.jl

KL-divergence estimation in Julia
Julia
7
star
48

TicTacToe.jl

Use Reinforcement Learning to teach a computer to play Tic-Tac-Toe
Julia
6
star
49

projecteuler.jl

Solutions to Project Euler problems in Julia
Julia
6
star
50

bayesian_canabalt

A Bayesian exploratory analysis of Canabalt scores.
R
6
star
51

JAGSIntro

A short intro to probabilistic programming using JAGS
R
6
star
52

TypeTree.jl

An interactive visualization of the Julia type hierarchy
JavaScript
6
star
53

regularization_slides

An introduction to statistical regularization
6
star
54

NaiveBayes.jl

Naive Bayes models in Julia
Julia
5
star
55

NearestNeighbors.jl

Data structures for nearest neighbor search
Julia
5
star
56

Proximal.jl

Translation of Parikh and Boyd code for proximal algorithms
Julia
5
star
57

SortableHTMLTables

A package for writing data frames as sortable HTML tables.
JavaScript
4
star
58

Loss.jl

Loss functions
Julia
4
star
59

senate_analyses

Statistical Analyses of the U.S. Senate
R
4
star
60

webcast_examples

Examples for O'Reilly webcast
R
4
star
61

utils.jl

Utility functions for Julia + R compatibility wrapper
Julia
4
star
62

bayesian-decryption

An example R program that decrypts text encrypted using a substitution cipher.
R
4
star
63

IntertemporalChoice

An R package for modeling intertemporal choice data.
4
star
64

stringops

An R library for manipulating strings.
4
star
65

DCStats.jl

IJulia Notebooks that provide a brief introduction to Julia for statistical programming
4
star
66

cran-function-usage-analysis

Code to spider CRAN, tokenize package source code and analyze function usage both within and across packages.
3
star
67

fastR

Examples of writing faster R code.
R
3
star
68

CSVReaders.jl

A CSV parsing toolkit for Julia
Julia
3
star
69

julia_nyhackr

Slides about Optimization in Julia for NY R Meetup
R
3
star
70

GiltTalk

Slides for course at Gilt
R
3
star
71

julia_package_system

A package system for Julia.
Julia
3
star
72

UCSD2014Talk

Talk to Bradley Voytek's Data Science class at UCSD
R
3
star
73

FourierDescriptors

Generate images using Fourier descriptors.
R
3
star
74

NYCMarathon

Statistical Analyses of the NYC Marathon
R
3
star
75

DataGotham.jl

IJulia Notebook with material for DataGotham tutorial on using Julia for data analysis
3
star
76

cran_analysis

Analyses of CRAN packages
R
3
star
77

SimpleML.jl

Textbook implementations of ML algorithms in Julia using Optim.jl
Julia
3
star
78

CategoricalData.jl

Tools for working with categorical/ordinal data
Julia
3
star
79

DataFrameDemo

A demo of DataFrame's in Julia.
3
star
80

Ridge.jl

Ridge regression and classification
Julia
3
star
81

Wordle.jl

A Julia library for solving Wordle puzzles.
Jupyter Notebook
3
star
82

fmri_utilities

A set of basic utility methods for analyzing fMRI data in R.
3
star
83

ProbabilisticModeling.jl

A BUGS-like language for describing probabilistic models in Julia
Julia
2
star
84

Roxygen.jl

A Roxygen-like documentation package
Julia
2
star
85

FiniteDiff.jl

A replacement for the finite-differencing code in Calculus.jl
Julia
2
star
86

abbReviations

Functions for dealing with common abbreviations.
R
2
star
87

winsorization_simulation_study

A simulation study of winsorization
R
2
star
88

NHSTExamples

Examples of how NHST really works
R
2
star
89

PackageTesting.jl

A standard for testing Julia packages
Julia
2
star
90

DesignSpace.jl

Show part of the design space for NullableArrays
Julia
2
star
91

r_social_dynamics

Quantitative data on the social dynamics of the R community.
R
2
star
92

HyperLogLog.jl

HyperLogLog in Julia
Julia
2
star
93

Calculus2.jl

A draft of a new interface for the Calculus package
Julia
2
star
94

sf_politics

Endorsement data for SF politics
R
2
star
95

ML4H.jl

Machine Learning for Hackers in Julia
Julia
2
star
96

SGD.jl

Fit models using SGD in Julia
Julia
2
star
97

clean

A utility to recursively purge directories of all files identified by a naming convention.
2
star
98

ggplots

A wrapper library that provides functions for common plots using ggplot2.
R
2
star
99

ncclab

Rails code for the NCC Lab's website.
JavaScript
2
star
100

stroop

The classic Stroop task in R.
R
2
star