daroczig / logger

A lightweight, modern and flexible, log4j and futile.logger inspired logging utility for R
https://daroczig.github.io/logger
280 stars 41 forks source link

Logging to file writes coloring symbols when I want to see plain text from file #101

Closed alevax closed 2 years ago

alevax commented 2 years ago

When I run this basic code to log both console and sdtout, the txt file contains also other symbols used to color output in console:

INFO [2022-01-18 17:55:05] Loading Data

When instead I'd like to see only the text.

This is the code:

log_appender(appender = appender_tee("log.txt))
log_threshold(DEBUG)
log_info('Loading Data')
> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Monterey 12.1

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats4    stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] reprex_2.0.1         RColorBrewer_1.1-2   org.Mm.eg.db_3.14.0 
 [4] org.Hs.eg.db_3.14.0  AnnotationDbi_1.56.2 IRanges_2.28.0      
 [7] S4Vectors_0.32.3     Biobase_2.54.0       BiocGenerics_0.40.0 
[10] forcats_0.5.1        stringr_1.4.0        dplyr_1.0.7         
[13] purrr_0.3.4          readr_2.1.1          tidyr_1.1.4         
[16] tibble_3.1.6         ggplot2_3.3.5        tidyverse_1.3.1     
[19] crayon_1.4.2         matrixStats_0.61.0   limma_3.50.0        
[22] logger_0.2.2         Seurat_4.1.0         SeuratObject_4.0.4  
[25] SeuratData_0.2.1    

loaded via a namespace (and not attached):
  [1] clipr_0.7.1                 utf8_1.2.2                 
  [3] reticulate_1.23             tidyselect_1.1.1           
  [5] RSQLite_2.2.9               htmlwidgets_1.5.4          
  [7] grid_4.1.2                  Rtsne_0.15                 
  [9] devtools_2.4.3              munsell_0.5.0              
 [11] codetools_0.2-18            ica_1.0-2                  
 [13] future_1.23.0               miniUI_0.1.1.1             
 [15] withr_2.4.3                 colorspace_2.0-2           
 [17] highr_0.9                   knitr_1.37                 
 [19] rstudioapi_0.13             ROCR_1.0-11                
 [21] tensor_1.5                  listenv_0.8.0              
 [23] labeling_0.4.2              MatrixGenerics_1.6.0       
 [25] GenomeInfoDbData_1.2.7      polyclip_1.10-0            
 [27] farver_2.1.0                bit64_4.0.5                
 [29] rprojroot_2.0.2             parallelly_1.30.0          
 [31] vctrs_0.3.8                 generics_0.1.1             
 [33] xfun_0.29                   R6_2.5.1                   
 [35] GenomeInfoDb_1.30.0         bitops_1.0-7               
 [37] spatstat.utils_2.3-0        cachem_1.0.6               
 [39] DelayedArray_0.20.0         assertthat_0.2.1           
 [41] promises_1.2.0.1            scales_1.1.1               
 [43] gtable_0.3.0                globals_0.14.0             
 [45] processx_3.5.2              goftest_1.2-3              
 [47] rlang_0.4.12                splines_4.1.2              
 [49] lazyeval_0.2.2              spatstat.geom_2.3-1        
 [51] broom_0.7.11                BiocManager_1.30.16        
 [53] yaml_2.2.1                  reshape2_1.4.4             
 [55] abind_1.4-5                 modelr_0.1.8               
 [57] backports_1.4.1             httpuv_1.6.5               
 [59] tools_4.1.2                 usethis_2.1.5              
 [61] ellipsis_0.3.2              spatstat.core_2.3-2        
 [63] sessioninfo_1.2.2           ggridges_0.5.3             
 [65] Rcpp_1.0.8                  plyr_1.8.6                 
 [67] sparseMatrixStats_1.6.0     zlibbioc_1.40.0            
 [69] RCurl_1.98-1.5              ps_1.6.0                   
 [71] prettyunits_1.1.1           rpart_4.1-15               
 [73] deldir_1.0-6                pbapply_1.5-0              
 [75] cowplot_1.1.1               zoo_1.8-9                  
 [77] SummarizedExperiment_1.24.0 haven_2.4.3                
 [79] ggrepel_0.9.1               cluster_2.1.2              
 [81] fs_1.5.2                    magrittr_2.0.1             
 [83] data.table_1.14.2           RSpectra_0.16-0            
 [85] glmGamPoi_1.6.0             scattermore_0.7            
 [87] lmtest_0.9-39               RANN_2.6.1                 
 [89] fitdistrplus_1.1-6          anndata_0.7.5.3            
 [91] pkgload_1.2.4               evaluate_0.14              
 [93] hms_1.1.1                   patchwork_1.1.1            
 [95] mime_0.12                   xtable_1.8-4               
 [97] readxl_1.3.1                gridExtra_2.3              
 [99] testthat_3.1.1              compiler_4.1.2             
[101] KernSmooth_2.23-20          htmltools_0.5.2            
[103] mgcv_1.8-38                 later_1.3.0                
[105] tzdb_0.2.0                  lubridate_1.8.0            
[107] DBI_1.1.2                   dbplyr_2.1.1               
[109] MASS_7.3-55                 rappdirs_0.3.3             
[111] Matrix_1.4-0                cli_3.1.0                  
[113] parallel_4.1.2              igraph_1.2.11              
[115] GenomicRanges_1.46.1        pkgconfig_2.0.3            
[117] plotly_4.10.0               spatstat.sparse_2.1-0      
[119] xml2_1.3.3                  XVector_0.34.0             
[121] rvest_1.0.2                 n1platform_1.3.17          
[123] callr_3.7.0                 digest_0.6.29              
[125] sctransform_0.3.3           RcppAnnoy_0.0.19           
[127] Biostrings_2.62.0           spatstat.data_2.1-2        
[129] rmarkdown_2.11              cellranger_1.1.0           
[131] leiden_0.3.9                uwot_0.1.11                
[133] DelayedMatrixStats_1.16.0   curl_4.3.2                 
[135] shiny_1.7.1                 lifecycle_1.0.1            
[137] nlme_3.1-155                jsonlite_1.7.3             
[139] desc_1.4.0                  viridisLite_0.4.0          
[141] fansi_1.0.2                 pillar_1.6.4               
[143] lattice_0.20-45             KEGGREST_1.34.0            
[145] fastmap_1.1.0               httr_1.4.2                 
[147] pkgbuild_1.3.1              survival_3.2-13            
[149] glue_1.6.0                  remotes_2.4.2              
[151] png_0.1-7                   bit_4.0.4                  
[153] stringi_1.7.6               blob_1.2.2                 
[155] memoise_2.0.1               irlba_2.3.5                
[157] future.apply_1.8.1  
daroczig commented 2 years ago

I suppose this happens with layout_glue_colors -- in that case, I'd not suggest using appender_tee, but instead set up layout_glue_colors for the console and something else (e.g. layout_glue) for the file using index. See https://daroczig.github.io/logger/articles/customize_logger.html#stacking-loggers for more details, and feel free to reopen if you think that doesn't address your problem.

alevax commented 2 years ago

Thanks for pointing it out. I used the namespace parameter and it looks like it works for the purpose of having a colored output in the console and plain text in the log file. Here is the code I used:

log_threshold(DEBUG,namespace = "file")
log_appender(appender = appender_file("log.txt),namespace = "file")

log_threshold(DEBUG,namespace = "console")
log_formatter(formatter_glue,namespace = "console")

log_layout(layout_glue_colors,namespace = "console")
log_warn('Loading Data',namespace="console")
log_warn('Loading Data',namespace="file")

However, I don't know if what I expect is a missing feature or me not knowing how to do it, but I guess it would be nice to have one log function that writes both to file and console (colored). With appender_tee it looks like it is possible to do so, but without the coloring output to console, since it would add to file a lot of unwanted symbols.

Is there a way to reach this goal or is the feature not implemented (i.e., one function to log both file and colored console)?

daroczig commented 2 years ago

Yeah, it's implemented: look at the index parameter instead of the namespace.

alevax commented 2 years ago

Wonderful! Thanks a lot, @daroczig.

It was very straightforward:

library(logger)
log_threshold(DEBUG)
log_threshold(DEBUG,index=2)
log_appender(appender = appender_file("log.txt"),index=2)
log_layout(layout_glue_colors)
log_warn('Ciao Bello')