Pine Script Debugging & Logging

Pine Script Console Log: How to Print Variables & Debug Code

Pine Script does not feature a JavaScript-style console.log() function. Instead, TradingView provides the dedicated log namespace: log.info(), log.warning(), and log.error(). Messages printed with these functions appear inside the Pine Logs pane at the bottom of the Pine Editor, allowing developers to inspect variable values, bar states, and execution timestamps.

Debug Pine Script with AI

How logging works in TradingView Pine Script

Because Pine Script computes bar-by-bar, placing an unconditional log statement prints a message on every historical chart bar. To avoid hitting the 10,000 message cap, developers wrap log statements inside conditional blocks or barstate checks.

FunctionSeverity levelColor in Pine LogsIntended purpose
log.info(message)InformationalNeutral / GrayTracking variable state, entry triggers, and parameter setups
log.warning(message)WarningAmber / OrangeUnexpected inputs, zero divisions, or edge conditions
log.error(message)ErrorRedCritical calculation failures or invalid trading parameter states

Complete Pine Script v6 script with structured console logging

The following Pine Script v6 indicator demonstrates how to log entry conditions, print variable values with formatted text, and guard against excessive log output using barstate filters.

Pine Script v6: Structured debugging with log.info and log.warningpinescript
//@version=6
indicator("Debug Logger Example [v6]", overlay = true)

// Input parameters
int fastLength = input.int(9, "Fast MA Length", minval = 1)
int slowLength = input.int(21, "Slow MA Length", minval = 1)

// Sanity check input parameters
if fastLength >= slowLength
    log.warning("Fast length ({0}) is greater than or equal to slow length ({1})", fastLength, slowLength)

// Calculations
float fastMa = ta.ema(close, fastLength)
float slowMa = ta.ema(close, slowLength)
bool bullCross = ta.crossover(fastMa, slowMa)
bool bearCross = ta.crossunder(fastMa, slowMa)

// Visual plots
plot(fastMa, "Fast EMA", color = color.green, linewidth = 2)
plot(slowMa, "Slow EMA", color = color.red, linewidth = 2)

// Conditional logging on signal events
if bullCross
    log.info("Bullish Cross on bar index {0} | Close: {1} | Fast MA: {2}", 
             bar_index, str.tostring(close, "#.##"), str.tostring(fastMa, "#.##"))

if bearCross
    log.info("Bearish Cross on bar index {0} | Close: {1} | Slow MA: {2}", 
             bar_index, str.tostring(close, "#.##"), str.tostring(slowMa, "#.##"))

// Log summary once on the latest confirmed bar
if barstate.islastconfirmedhistory
    log.info("Historical calculation complete. Total bars processed: {0}", bar_index + 1)

Console log vs on-chart debug methods

TradingView supports multiple complementary techniques for viewing variable data during indicator and strategy development:

  • Pine Logs (log namespace): Best for sequential event tracing, timestamped order triggers, and detailed variable dumps across historical bars.
  • Data Window & plot(): Best for inspecting continuous numeric series (like RSI or moving averages) on any historical bar by hovering the cursor.
  • On-chart labels (label.new): Best for pinning text and prices directly to specific candlesticks for instant visual confirmation.
  • Diagnostic tables (table.new): Best for persistent heads-up dashboard panels displaying live calculations in a chart corner.

Best practices for clean Pine Script debugging

Avoid common logging pitfalls that clutter output or halt script execution:

  • Filter with barstate: Use barstate.isconfirmed or barstate.islast to avoid repetitive logs fired on every intrabar price tick.
  • Use str.format for clean templates: Instead of chaining string additions with plus signs, use str.format("Index: {0}, Close: {1}", bar_index, close).
  • Clear logs before runs: Open Pine Logs and click the trash icon before adjusting inputs to keep old script outputs separate.
  • Disable verbose logs in production: Comment out or guard heavy log statements before sharing public scripts with other traders.
Where Pineify fits

Pine Script AI Coding Agent

Diagnose runtime bugs, inspect logic branching, and generate clean Pine Script v6 scripts with automated debugging from Pineify AI.

Also useful: Visual Pine Editor. Build indicators without worrying about syntax errors or missing variable declarations.

Debug Pine Script with AI

Frequently asked questions

Debugging logs and on-chart diagnostics assist in script development and code validation. Always test execution latency and signal timing before using automated strategy alerts in live accounts.

Sources and verification